Skip to content

Instantly share code, notes, and snippets.

@qmmr
Last active August 29, 2015 14:22
Show Gist options
  • Save qmmr/afd5beb2c451040b3510 to your computer and use it in GitHub Desktop.
Save qmmr/afd5beb2c451040b3510 to your computer and use it in GitHub Desktop.
ES6 notes

Strings

String.prototype.includes(txt, start)

It was meant to be String.prototype.contains like the Array.prototype.contains but it would break MooTools

let title = 'JS Futures in London!';
title.includes('JS') // true
title.includes('!') // true
title.includes('JS', 3) // false

String.prototype.startsWith(txt, start)

title.startsWith('JS') // true
title.startsWith('js') // false
title.startsWith('F', 3) // true

String.prototype.endsWith(txt, start)

title.endsWith('!') // true
title.endsWith('London!') // true
title.endsWith('!', 3) // false
title.endsWith('Lon', 6) // true

String.prototype.repeat(count)

console.log('x'.repeat(3)) // 'xxx'
console.log('hello'.repeat(2)) // 'hellohello'

String.prototype.trim()

console.log(' x  '.trim()) // 'x'
console.log('  \tx'.trim()) // 'x'
console.log('foo bar\n'.trim()) // 'foo bar'

Template strings

Instead of using different quotes(single, double) we can use backticks(grave accent) to have properly interpolated expressions

var name = 'Marcin'
console.log(`Hello, name is ${ name }! \nThis sentence will be on the new line.`);
console.log(`Hello, name is ${ name }!
              This will be on the new line as well!`);

Tagged Template String

You can use functions to make adjustments to the strings. TODO: update with dedent example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment