Mihai Serban

Cluj-Napoca, Romania 🇷🇴

Software engineer in constant search for new and exciting technologies

Want to hire me for your next project? Get in touch.

ES6 cheatsheet  —  Helpful string functions

Serban Mihai / 20 October 2018

~1 min read

ES6 string functions

.includes( )

var string = 'string';  
var substring = 'str';  

console.log(string.indexOf(substring) > -1);

Instead of checking for a return value > -1 to denote string containment, we can simply use .includes() which will return a boolean:

const string = 'string';  
const substring = 'str';  

console.log(string.includes(substring)); // true

.repeat( )

function repeat(string, count) {  
    var strings = [];  
    while(strings.length < count) {  
        strings.push(string);  
    }  
    return strings.join('');  
}

In ES6, we now have access to a nicer implementation:

// String.repeat(numberOfRepetitions)  
'str'.repeat(3); // 'strstrstr'

You can find a more complete ES6 cheetsheet on my Github page.

P.S. If you ❤️ this, make sure to follow me on Twitter, and share this with your friends 😀🙏🏻

gatsby  +  contentful  +  netlify  = ❤️[Source code]