I am splitting up a number into individual digits and placing them in an array:
var number = 123456789; //number I want to split
var output = []; //empty output array
var sortedNumber = number.toString(); //converting number to string
var len = sortedNumber.length;
for (var i = 0; i < len; i += 1) {
output.push(sortedNumber.charAt(i)); //Digits pushed into empty output array
}
console.log(output);
When I console.log the array, the array outputs the digits as strings, rather than actual numbers:
["1", "2", "3", "4", "5", "6", "7", "8", "9"]
However, when I adjust my code to:
output.push(+sortedNumber.charAt(i));
.. the array outputs the digits as actual numbers, which is what I want.
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Why is this the case? What does the '+' do that means the array is full of numbers rather than strings? I am new to programming so any explanation would be greatly appreciated. Thanks in advance.