Javascript – How to join an array of numbers into 1 concatenated number


How do I join this array to give me expected output in as few steps as possible?

var x = [31,31,3,1]
//expected output: x = 313131;

Best Solution

Use array join method.Join joins the elements of an array into a string, and returns the string. The default separator is comma (,). Here the separator should be an empty string.

var  x = [31,31,3,1].join("");

EDIT: To get the result as numeric

const  x = +[31,31,3,1].join("");


const  x = Number([31,31,3,1].join(""));