vince
vince

Reputation: 81

How to extract digits/numbers of an array one by one (outside of a loop) and not into another array (raw numbers)? Javascript

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

Answers (1)

Saar Davidson
Saar Davidson

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}`);

You can red more about it here

Destructuring assignment

Upvotes: 1

Related Questions