JS-ES6 Important String Methods

JS-ES6 Important String Methods
----------------------------------------------

In this topic we will cover ES6 important string method which is String startsWith() | String endsWith() | String includes()

We will cover all ES6 sting methods in this detail.

Introduction to the JavaScript startsWith() method

The startsWith() method returns true if a string starts with a substring or it will return false otherwise.

startsWith() method is case-sensitive.

Syntax

string.startsWith(searchString, start)

Parameters

  • searchString = Required. The string to are searching for.
  • start             = Optional. Start position. The default is 0.

Examples

const text = "Hello, Welcome to coderuck";
console.log(text.startsWith("Hello"));

Output will be true 

Now we will check by using the position

const text = "Hello, Welcome to coderuck";
console.log(text.startsWith("Hello",2));

Output will be false

 

Introduction to the JavaScript endsWith() method

The endsWith() method returns true if a string ends with a substring or it will return false otherwise.

endsWith() method is case-sensitive.

Syntax

string.endsWith(searchString, length)

Parameters

  • searchString = Required. The string to are searching for  at the end of the string. .
  • length          = Optional. The length of the string to search.
                                          Default value is the length of the string. 

Examples

const text = "Hello, Welcome to coderuck";
console.log(text.endsWith("coderuck"));

Output will be true 

Now we will check by using the second parameter that determines the length of the string to search: 

const text = "Hello, Welcome to coderuck";
console.log(text.endsWith("Hello",15));

Output will be false

 

Introduction to JavaScript includes() method

The includes() method returns true if a string contains another string or it will return false otherwise.

includes() method is case-sensitive.

Syntax

string.includes(searchString, start)

Parameters

  • searchString = Required. The string to are searching for.
  • start             = Optional. Start position. The default is 0.

Examples

const text = "Hello, Welcome to coderuck";
console.log(text.includes("Welcome"));

Output will be true 

Now we will check by using the position

const text = "Hello, Welcome to coderuck";
console.log(text.includes("Welcome ",15));

Output will be false

 

Categories: Java Script Tags: #ES6, #JavaScript,

Newsletter Subcribe

Receive updates and latest news direct from our team. Simply enter your email.