Brand New JavaScript ES6 String Methods that you should Know?


Hello, Friends, I know most of the developers are now switching to es6 or they have already switched.

ES6 looks little complicated but it is easy at last now javascript also possessing all the features similar to python etc

I'm writing this article to show you a few powerful string methods that we developers got as a gift from the ES6.

String methods like trim(), the index based accessing etc are very popular now which was introduced in the ES5 (ECMAScript 5)

Today I will show you three new string methods that are introduced with ES6. They are as follows

  1. startsWth()
  2. endsWith()
  3. repeat()

The above are the three new string methods.  I will explain them with an example one by one starting from the top to bottom.

startsWith(): This method is case sensitive. this method accepts two parameters. First one is required and the second one is optional.

Syntax:
let example = "Hello, Developer. How are you?";
example.startsWith(searchString , position);

This method returns boolean true or false. if your search matches the string starting characters then it will return true otherwise false.

Arguments: 

The First one is Required *. Here you have to add the starting characters of the string that you are searching.


    The Second one is optional here you have to specify the index position to starts searching from.

    Example of a startsWith method:

    See the Pen String Methods in ES6 by MD M Nauman (@mmnauman) on CodePen.






    This was the simple example of a startsWith method. You can add the second parameter to specify the index position to starts searching from there.

    The used case of startsWith:

    It can be used in many things from filtering out the names based on their first name,

    You can also allow only people whose name starts with certain characters to login in your site or do some other stuff.

    There are so many things that can be done using this method.

    It will replace complex regex methods to find with a certain pattern.

    Play with this in your developer tools to get the instant return value.

    endsWith(): This method also possesses similar features that we saw above in startsWith method.

    This method is case-sensitive. It also accepts two parameters.

    The first Parameter is required and the second one is optional.

    Syntax:
    let example = "Hello, Developer. How are you?";
    example.startsWith(searchString , length);

    This method returns boolean true or false. if your search matches the string starting characters then it will return true otherwise false.

    Arguments: 

    The First one is Required *. Here you have to add the starting characters of the string that you are searching.

    The Second one is optional. This second parameter is different than the startsWith method. Here you have to specify the length of the string within that you want to search your characters.

    Example of an endsWith method:


    See the Pen String Methods in ES6 endsWith() by MD M Nauman (@mmnauman) on CodePen.



    The used case of endsWith() Method:

    This is a very powerful string method and can be used in many things like:

    To find out the emails ending with .com, .net etc
    To find out whether the person last name is 
    To find out whether the  image is jpg/png etc

    There are many possibilities you can implement this method in your projects. 

    repeat(): This method creates copies of the string and you have to specify the number as the argument to create your desired amount of copies. This method doesn't modify the original string but it will return the new string with the specified copies of the string.

    repeat method accepts one parameter. The Parameter is required

    Syntax:
    let example = "Hello";
    example.repeat(5);
    Arguments: 

    The argument is Required *. Here you have to add the number so that the repeat method will create that many copies of the string for you.

    Example of the repeat method:


    See the Pen String Methods in ES6 repeat() by MD M Nauman (@mmnauman) on CodePen.



    The used case of repeat Method:

    This method is rarely used as it is not that useful.

    You can use this method in your development to generate dummy text.

    Conclusion

    I'm concluding this article now. This is the brand new methods that were introduced with ES6. If you found it little difficult then try it again and again till you get used to with them.

    With this new methods, I think the development complexity will be reduced. More then that javascript is becoming powerful day by day.

    ES6 is a new standard for JavaScript. Leaving Internet Explorer most modern browsers support all these methods. and gradually all browsers have to comply with this standard.

    That's It Thanks For Following our Tutorial ''Brand New JavaScript ES6 String Methods that you should Know?' Along With us If you found any difficulty Please Comment and Share Your Valuable Opinion. And Stay tuned for More Tutorials Like This and Share this with your friends.

    No comments:

    Post a Comment