Reputation: 81
var num = 123;
var digits = num.toString().split('');
var arrayDigits = digits.map(Number)
console.log(arrayDigits);
In the next step, how can I extract values/numbers one by one out of arrayDigits after a loop and use each value (as number) outside of a forloop and array?
Upvotes: 0
Views: 629
Reputation: 1382
you can use array destructuring and extract the variables you want like so:
var num = 123;
var digits = num.toString().split('');
var arrayDigits = digits.map(Number);
var [singles, tens, hundreds] = arrayDigits;
console.log(`${singles} ${tens} ${hundreds}`);
Upvotes: 1