ES6 – Check if a string contains a substring

Hi, in this post I’m going to show you the new ways to work with strings that ES6 implemented.

If we need to know (true or false) is some string have some string or specific character

const myString = "Cristina Elizabeth Rojas Zamora";

console.log(myString.includes("Cristina"));

Result:

Upper or lower case does it matter? the answer is yes, if you try to find “rojas” (lower case) then we will have false.

const myString = "Cristina Elizabeth Rojas Zamora";

console.log(myString.includes("rojas"));

Result:

Nice, now if we want to start searching from specific character then we can insert a second parameter in .includes( ) method

const myString = "Cristina Elizabeth Rojas Zamora";

console.log(myString.includes("liza", 10));

Result:

And this is because the main string start from 0 and continue, then we define the specific position in .includes() method and this method will start searching from that position forward.

Note: if you have myString.includes(“liza”, 11) this will be false.