JS - Best Practices (String)


Use single quotes '' for strings.

// bad
const name = "My String";

// bad - template literals should contain interpolation or newlines
let text = `This string contains "double quotes" which don't need to be escaped anymore.`;     

// good
const name = 'My String';


.includes( )

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

const string = 'food';
const substring = 'foo';
console.log(string.includes(substring)); // true  


.repeat( )

// String.repeat(numberOfRepetitions)
'meow'.repeat(3); // 'meowmeowmeow'  


js best practices