4

Quick JS tip

If you want to split a string into characters, you can use the ES6 spread operator

If you have
const name = 'react-dev'

You can just spread it like this:
const charsInName = [...name]

Comments
  • 0
    Same with other iterables such as arrays :)
    It's super useful!

    https://developer.mozilla.org/en-US...
  • 1
    It's also nice if you want to manipulate the first char. Only putting the string back together is a bit awkward to me

    const [first,...rest] = "Hello World"
    return first.toLowerCase() + rest.join('')
  • 0
    @host127001
    Interesting, that's a much cleaner approach than the one I made for capitalizing the first letter of a string:

    stringVariable.toLowerCase().replace(/^(.)|\s(.)/g, ($1) => $1.toUpperCase()))

    ^ That looks pretty ugly hahah
  • 1
    @hacker your solution uses regex and more regex is always better. I like it
  • 0
    @host127001 hahah and yours is an ES6 beauty. I like it, too :D
Add Comment