Tuesday, September 24, 2024
Google search engine
HomeLanguagesJavascriptES6 New String Methods

ES6 New String Methods

In ES6, four new methods were added to String. These methods are like a boon for programmers when it comes to string manipulation in JavaScript. In day to day programming, we often deal with strings. The first three methods also reduce the dependency on Regular Expression RegExp for certain tasks. Four ES6 New String Methods are described below:

  1. startsWith( queryString, position ) Method: This method returns either true, if the string starts with the provided string from the specified position, otherwise false. This method is case-sensitive. This method accepts two arguments :
    • queryString: The string which has to be searched at the beginning of the String.
    • position (optional): The position from where the search has to be start. Please note that its default value is 0.

    Below example illustrates the first method of ES6 New String Methods (startsWith(queryString, position)).

    Example:




    < script >
    let str = "neveropen";
      
    console.log(str.startsWith("Geeks"));
      
    // Here specified position is 5, that means
    // searching will start from 'f' whose index
    // in string str is 5
    console.log(str.startsWith("for", 5));
      
    console.log(str.startsWith("neveropen")); 
    < /script>

    
    

    Output:

    true
    true
    false
    
  2. endsWith( queryString, length ) Method: This method returns either true, if the string ends with the provided string for specified length, otherwise false. This method is case-sensitive. This method accepts two arguments:
    • queryString: The string which has to be searched at the end of the String.
    • length (optional): The length of the string. Please note that its default value is length of the string.

    Below example illustrates the second method of ES6 New String Methods (endsWith(queryString, length)).

    Example:




    <script>
    let str = "neveropen";
      
    console.log(str.endsWith("Geeks"));
      
    // Here specified length is 8, that means
    // length of str will be considered as 8
    // and rest will be omitted
    console.log(str.endsWith("for", 8));
      
    console.log(str.endsWith("neveropen"));
    </script>

    
    

    Output:

    true
    true
    false
    
  3. includes( queryString, position ) Method: This method returns either true if the string is present in the provided string, otherwise returns false. This method is case-sensitive. This method accepts two arguments:
    • queryString: The string which is to be searched in the String.
    • position (optional): The position from where the search has to be start. Please note that its default value is 0.

    Below example illustrates the third method of ES6 New String Methods (includes(queryString, position)).

    Example:




    <script>
    let str = "neveropen";
      
    console.log(str.includes("eks"));
      
    // Here search will start from index 8
    // of str
    console.log(str.includes("for", 8));
      
    console.log(str.includes("neveropen"));
    </script>

    
    

    Output:

    true
    false
    false
    
  4. repeat( count ) Method: This method accepts single argument which is an integer value that represents the number of times the string is to be repeated. This method returns the newly created string with ‘count’ times repeated old string.

    Note: The argument provided i.e. count must be an positive integer.

    Below example illustrates the fourth method of ES6 New String Methods (repeat(count)).

    Example:




    <script>
    let str = "neveropen";
    console.log(str.repeat(2));
    let newStr = str.repeat(3);
    console.log(newStr);
    </script>

    
    

    Output:

    neveropenneveropen
    neveropenneveropenneveropen
    
  5. Template Literals: These literals allowed embedded expression. Single or double quotes are not it’s behavior rather that it used back-ticks “`”. There are two types of Template Literals.

  • Singleline Strings and Template Literals: The Singleline Strings and Template Literals contains the single line string below example will illustrate this.

    Example:




    <script>
    var a = "Geeks"
    var b = "for"
    console.log(`We are the ${a+b+a} `)
    </script>

    
    

    Output:

    We are the neveropen 
  • Multiline Strings and Template Literals: This Multiline Strings and Template Literals contains the multi-line string below example will illustrate this.

    Example:




    <script>
    var a = `neveropen`; 
    var b = ` A Online 
    Computer Science 
    Portal for Geeks`; 
      
    console.log(a + b)
    </script>

    
    

    Output:

    neveropen A Online 
    Computer Science 
    Portal for Geeks

String.raw() Method: The String.raw() method is used to print the backslash as it is and it does not bring the new line into the console.

Example:




<script>
var neveropen = String.raw `A Online Computer \nScience Portal for Geeks`
  
//Printing backslash as string
console.log(neveropen)
</script>


Output:

A Online Computer \nScience Portal for Geek

String.fromCodePoint() Method: The String.fromCodePoint() is an inbuilt function in JavaScript which is used to return a string or an element for the given sequence of code point value (ASCII value).

Example:




<script> 
  
    // Taking some code point values 
    a = String.fromCodePoint(42); 
    b = String.fromCodePoint(66, 65, 102); 
  
    // Printing the corresponding elements of 
    // the code point value. 
    console.log(a + "<br>"
    console.log(b) 
  
</script> 


Output:

*
BAf

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments