Other String Changes

JavaScript strings have always lagged behind similar features of other languages. It was only in ECMAScript 5 that strings finally gained a trim() method, for example, and ECMAScript 6 continues extending JavaScript’s capacity to parse strings with new functionality.

Methods for Identifying Substrings

Developers have used the indexOf() method to identify strings inside other strings since JavaScript was first introduced. ECMAScript 6 includes the following three methods, which are designed to do just that:

  • The includes() method returns true if the given text is found anywhere within the string. It returns false if not.
  • The startsWith() method returns true if the given text is found at the beginning of the string. It returns false if not.
  • The endsWith() method returns true if the given text is found at the end of the string. It returns false if not.

Each methods accept two arguments: the text to search for and an optional index. When the second argument is provided, includes() and startsWith() start the match from that index while endsWith() starts the match from the second argument minus the length of the first argument; when the second argument is omitted, includes() and startsWith() search from the beginning of the string, while endsWith() starts from the end. In effect, the second argument minimizes the amount of the string being searched. Here are some examples showing these three methods in action:

  1. var msg = "Hello world!";
  2. console.log(msg.startsWith("Hello")); // true
  3. console.log(msg.endsWith("!")); // true
  4. console.log(msg.includes("o")); // true
  5. console.log(msg.startsWith("o")); // false
  6. console.log(msg.endsWith("world!")); // true
  7. console.log(msg.includes("x")); // false
  8. console.log(msg.startsWith("o", 4)); // true
  9. console.log(msg.endsWith("o", 8)); // true
  10. console.log(msg.includes("o", 8)); // false

The first six calls don’t include a second parameter, so they’ll search the whole string if needed. The last three calls only check part of the string. The call to msg.startsWith("o", 4) starts the match by looking at index 4 of the msg string, which is the “o” in “Hello”. The call to msg.endsWith("o", 8) starts the search from index 7 (the second argument minus the length of the first argument), which is the “o” in “world”. The call to msg.includes("o", 8) starts the match from index 8, which is the “r” in “world”.

While these three methods make identifying the existence of substrings easier, each only returns a boolean value. If you need to find the actual position of one string within another, use the indexOf() or lastIndexOf() methods.

W> The startsWith(), endsWith(), and includes() methods will throw an error if you pass a regular expression instead of a string. This stands in contrast to indexOf() and lastIndexOf(), which both convert a regular expression argument into a string and then search for that string.

The repeat() Method

ECMAScript 6 also adds a repeat() method to strings, which accepts the number of times to repeat the string as an argument. It returns a new string containing the original string repeated the specified number of times. For example:

  1. console.log("x".repeat(3)); // "xxx"
  2. console.log("hello".repeat(2)); // "hellohello"
  3. console.log("abc".repeat(4)); // "abcabcabcabc"

This method is a convenience function above all else, and it can be especially useful when manipulating text. It’s particularly useful in code formatting utilities that need to create indentation levels, like this:

  1. // indent using a specified number of spaces
  2. var indent = " ".repeat(4),
  3. indentLevel = 0;
  4. // whenever you increase the indent
  5. var newIndent = indent.repeat(++indentLevel);

The first repeat() call creates a string of four spaces, and the indentLevel variable keeps track of the indent level. Then, you can just call repeat() with an incremented indentLevel to change the number of spaces.

ECMAScript 6 also makes some useful changes to regular expression functionality that don’t fit into a particular category. The next section highlights a few.