Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Ecma 1
  • Overview
  • Transcript

3.1 New Methods of the String Object

There have been a number of new methods added to the built-in types, such as String, Number and Array. In this lesson, we'll look at some of the new methods for the String object.

3.1 New Methods of the String Object

Hi folks. In this lesson we're going to see what new lessons ES6 brings to string objects. Babel supports a lot of these new methods using the Babel polyfill. The polyfill is super easy to install, we just need to head over to the command line and install it via mpm. Once that's installed, we just need to link to a file that gets installed within our HTML page. So we just need to make sure that the polyfill is loaded before we try to use any ES6 features. And we can also switch back to the es6.js file because now that we're using the polyfill, we don't need to transpile. And we'll stick with this format for the next few lessons. So let's make a start by looking at some new string methods. ES6 provides a couple of convenience methods for determining whether a string starts with or ends with another string. These methods are the starts with and ends with methods and are both instance methods with strings that we define. So, let's take a look in our browser. And we see that both of those are true. Both of these methods can also accept an optional second parameter that limits the portion of the string that gets searched. And now the second log is false. Because the last character of the string is excluded from the search. The second parameter of the starts with method is similar, except that it specifies the character to begin searching from. And now, the first log is false as well. Another method we can use for searching within strings is includes. With this method, the specified string just has to be inside of the target string. In this case the string STR is within the string test string, and so the includes method returns true. This method also accepts a position argument and is the position to start searching from. So now the log changes to false. Another new instance method is repeat, which we can use to repeat the string a specified number of times. The repeat method accepts a numerical argument. And this argument dictates how many times the string instance is repeated. This argument must be greater than zero and less than infinity. Other string methods bought by ES6 are for working with Unicode. We have the static from code point method for creating a string from one or more code points. And the instance methods code point at forgetting the code point at a specified position in the string and normalize, which returns the Unicode normalization form of the string that it's called on. So in this lesson we learned about the new methods that we can use with strings in ES6. We saw that there is a single new static method fromCodePoint() which creates a new string from a Unicode code points, and a range of new instance methods that we can call on individual strings that we've created. We saw that there are now new methods for searching within strings, startsWith(), endsWith(), and includes(). We also saw that we can use the repeat method to repeat a string a specified number of times. Thanks for watching.

Back to the top