ArtemKha
ArtemKha

Reputation: 869

How to concatenate many numbers in javascript?

I'd like to return numbers 5 6 7 in one line into the console instead of the string of "5 6 7". I want numbers as if I put console.log(5, 6, 7). But if I have an array of many numbers with console.log([5, 6, 7].join(' ')) it returns a string.

Upvotes: 3

Views: 82

Answers (2)

Debsdoon
Debsdoon

Reputation: 51

I think you are close. Try joining values without any blank - console.log([5, 6, 7].join(''))

To verify that return value is integer and not string (Not sure why you want to do this), you can try console.log(([5,6,7].join(''))-1) and see.

Upvotes: -1

Duncan Thacker
Duncan Thacker

Reputation: 5188

Not totally clear what you're asking, but if you're using ES6 you can use the spread operator:

const myArray = [ 5, 6, 7 ];
console.log( ...myArray ); //use the es6 spread operator to turn the array into args

Otherwise you can use .apply() like this:

var myArray = [ 5, 6, 7 ];
console.log.apply( console, myArray ); //use myArray as the arguments

Upvotes: 9

Related Questions