ES 6 features - New String methods| Learn Es6 tutorials

ES6 Features String methods

Es6 introduced new methods of a String object. In this blog post, I will cover the new string methods with examples

Newly introduced String methods can be generic and Unicode methods

Generic methods

  • startsWith()
  • endsWith()
  • includes()
  • repeat()

String.prototype.startsWith() method example

startsWith() method returns 

true - if the passed string with search string is matched from the starting index position, If index position is specified, It checks at the nth position 

false - search string is not matched



The two arguments are 1. String to search -  substring to be searched 2. Index position - It is a position on which search starts from. By default is zero

const message = "This is a new string methods";
console.log(message.startsWith("This is")); // true
console.log(message.startsWith("Thias is")); // false
console.log(message.startsWith("is", 4)); // false
console.log(message.startsWith("is", 5)); // true

String.prototype.endsWith() method example

endsWith() method also  take two arguments

This method returns true if string end is matched with other string



It has two arguments 1.Search string - string to search that matched from end 2. Index position - It is a position to match with string, this is optional

const message = "This is a new string methods";
console.log(message.endsWith("methods")); // true
console.log(message.endsWith("methodsa is")); // false
console.log(message.endsWith("is", 4)); // true
console.log(message.endsWith("is", 5)); // false

String.prototype.includes() method example

This return true if the search substring contains in a string


It has two arguments 1.Search string - string to be matched with given string 2. Index position - It is starting position to begin searching the string, This is optional

const message = "this is tesing include";

console.log(message.includes("tesing")); // true
console.log(message.includes("adfadf")); // false
console.log(message.includes("is", 8)); // false
console.log(message.includes("is", 2)); // true

String.prototype.repeat() method example

This method repeats the given string for given number times. It gives the output concatenate strings


parameter - the number of times to repeat the string

const message = "cloud";
console.log(message.repeat(2)); // cloudcloud
console.log(message.repeat(3)); // cloudcloudcloud

Unicode methods

es6 introduced methods for dealing with Unicode strings. Here are the methods for String.fromCodePoint String.prototype.codePointAt; String.prototype.normalize;

Similar Posts