Home » ES6 Strings

ES6 Strings

JavaScript string is an object which represents the sequence of characters. Generally, strings are used to hold text-based values such as a person name or a product description.

In JavaScript, any text within the single or double quotes is considered as a string. There are two ways for creating a string in JavaScript:

  • By using a string literal
  • By using string object (using the new keyword)

Let us elaborate both ways of creating a string in JavaScript.

By using a string literal

The string literals can be created by using either double quotes or by using single quotes. The syntax for creating string literal is given below:

By using String object (using the new keyword)

Here, we will use a new keyword for creating the string object. The syntax for creating the string object is given below:

String Properties

There are some properties of the string that are tabulated as follows:

S.no. Property Description
1. constructor It returns the constructor function for an object.
2. length It returns the length of the string.
3. prototype It allows us to add the methods and properties to an existing object.

Let us discuss the above string properties in detail.

JavaScript string constructor property

The constructor property returns the constructor function for an object. Instead of the name of the function, it returns the reference of the function.

Syntax

Example

Output

Value of str.constructor is: function String() { [native code] }  

JavaScript string length property

As its name implies, this property returns the number of characters or the length of the string.

Syntax

Example

Output

The number of characters in the string str is: 11  

JavaScript string prototype property

It allows us to add new methods and properties in an existing object type. It is a global property which is available with almost all the objects of JavaScript.

Syntax

Example

Output

Daniel Grint  BCA  20  

ES6 String Methods

There are four string functions available in ES6, which are tabulated as follows:

S.no. Methods Description JavaScript Version
1. startsWith It determines whether a string begins with the characters of a specified string. ECMAScript 6
2. endsWith It determines whether a string ends with the characters of a specified string. ECMAScript 6
3. includes It returns true if the specified argument is in the string. ECMAScript 6
4. repeat It returns a new string repeated based on specified count arguments. ECMAScript 6

Let us discuss the above string methods in detail.

startsWith() method

It is a case-sensitive method, which determines whether the string begins with the specified string characters or not. It returns true if the string begins with the characters and returns false if not.

Syntax

This method includes two parameters that are as follows:

  • searchValue: It is the required parameter of this method. It includes the characters to be searched for at the start of the string.
  • startPosition: It is an optional parameter. Its default value is 0. It specifies the position in the string at which to begin searching.

Example

Output

true  false  

endsWith() method

It is also a case-sensitive method that determines whether a string ends with the characters of the specified string or not.

Syntax

The parameters of this method are defined as follows:

  • searchValue: It is the required parameter that represents the characters to be searched at the end of the string.
  • length: It is an optional parameter. It is the length of the string that will be searched. If this parameter is omitted, then the method will search in the full length of the string.

Example

Output

true  false  

includes() method

It is a case-sensitive method that determines whether the string contains the characters of the specified string or not. It returns true if the string contains characters and returns false if not.

Syntax

Let’s understand the parameters of this method.

  • searchValue: It is a required parameter. It is the substring to search for.
  • start: It represents the position where to start the searching in the string. Its default value is 0.

Example

Output

true  false  

repeat() method

It is used to build a new string that contains a specified number of copies of the string on which this method has been called.

Syntax

This function has a single argument.

  • count: It is a required parameter that shows the number of times to repeat the given string. The range of this parameter is from 0 to infinity.

Example

Output

hello worldhello worldhello worldhello worldhello world  

JavaScript String Methods

Let us see some of the JavaScript methods that are tabulated below:

S.no. Methods Description JavaScript Version
1. charAt() It provides the char value, which is present at the specified index. ECMAScript1
2. charCodeAt() It provides the Unicode character value, which is present at the specified index. ECMAScript1
3. concat() It provides a combination of two or more strings. ECMAScript1
4. match() It is used to search a specified regular expression in a given string and return that expression if a match happens. ECMAScript1
5. toString() It gives a string that represents the particular object. ECMAScript1
6. indexOf() It gives the position of the char value, which is present in the given string. ECMAScript1
7. lastIndexOf() It gives the char value position in the given string by searching a character from the last position. ECMAScript1
8. replace() It replaces the given string with the specified replacement. ECMAScript1
9. search() It searches a specific regular expression and returns its position if a match occurs. ECMAScript1
10. valueOf() It provides the primitive value of the string object. ECMAScript1
11. slice() It is used to fetch the part of the given string. ECMAScript1
12. split() It splits the string into the array of substring and returns the newly created array. ECMAScript1
13. substr() It fetches the part of the given string based on the specified length and starting position. ECMAScript1
14. substring() It fetches the part of the given string based on the specified index. ECMAScript1
15. toLocaleLowerCase() It converts the given string into the lowercase letter based on the current scale of the host. ECMAScript1
16. toLocaleUpperCase() It converts given string into uppercase letters based on the current scale of the host. ECMAScript1
17. toLowerCase() It simply converts the given string into the lowercase letter. ECMAScript1
18. toUpperCase() It simply converts the given string into the lowercase letter. ECMAScript1
19. trim() It trims the white space from the left and right side of the string. ECMAScript5

You may also like