ES6 string extension
In ES5, we learned indexOf() and lastIndexOf() to retrieve strings. ES6 has expanded three methods for us to retrieve strings, namely includes(), startsWith(), endsWith()
- includes()
returns a Boolean value, indicating whether the parameter string is found
let str = 'hello world';
console.log(str.includes('ell'));//true
- startsWith()
returns a Boolean value, indicating whether the parameter string is at the beginning of the source string
let str = 'hello world';
console.log(str.startsWith('hello'));//true
- endsWith()
returns a Boolean value, indicating whether the parameter string is at the end of the source string
let str = 'hello world';
console.log(str.endsWith('world'));//true
They can also carry a second parameter, which indicates where to start the search, such as:
let str = 'hello world';
console.log(str.includes('ell',1));//true
console.log(str.startsWith('world',6))//true
console.log(str.endsWith('hello',5))//true