Let's say we have a number:
let num = 969
I'm trying to split this number into an array of digits. The first two methods do not work, but the third does. What is the difference?
num + ''.split('') // '969'
num.toString() + ''.split('') // '969'
String(num).split('') // [ '9', '6', '9' ]
Well, let's look how it works
num + ''.split('') works like
num
is a number''.split('')
is empty array and it's not a numbernum
to string is '969', []
to string is '' (empty)num.toString() + ''.split('') works like
num.toString()
is a string[]
to
string[]
to string is '' (empty)String(num).split('') works like
num
to string