Strings are a part of almost any object-oriented programming language. Today, we’re going to discuss some of the basics of strings, and what new features are available to us with strings when using ES6.
Hello World). One other thing to note is that you can combine strings with variables
Hello World) , which are template literals. Strings also some interesting properties.
One property of strings is that you can combine them with variables (“Hello World” + variable1). Not only that, you can also perform operations on strings because strings are objects. In this case, we’ll be going over mostly the new operations introduced in ES6 that you can perform on strings.
The repeat() method allows you to repeat the output of the string. For example, if you had a string: “Hello World”, the repeat method would give you the output: “Hello WorldHello World”. This can be useful if you wanted to create a text repeater.
The includes() method searches a string for another string and returns true if it’s included in the string. This method is useful for searching for a particular word in a string, without using a regular expression to check for it.
The indexOf() method returns the index within the string of the first occurrence of another string, otherwise returns -1 if the value is not found. This isn’t a new feature of the language but can be useful for extracting a substring from another string or counting the occurrence of a substring within a string.
The startsWith() method returns true or false if the string starts with the specified sequence of characters. You can also change where in the string to check if the string starts with the specified sequence of characters. This can be useful if you want to check for a case sensitive sequence of characters at the start of your string.