How to convert the Number String to Number Array in JavaScript?

JavaScript

Follow the code below to convert the number string to Number Array.

let stringOfNumbers = "1,2,3";

Used the below code to convert that Number string to Number Array.

let numberArray = stringOfNumbers.map(x => Number(x));

let stringOfNumbers = "1,2,3";

// Show the String of Numbers 
document.getElementById("numberString").innerHTML = "The value of Number String is: " + stringOfNumbers;

// Split the String and Convert to Number format
let numberArray = stringOfNumbers.split(',').map(x => Number(x));

// Show the Number Array after conversion
document.getElementById("numberArray").innerHTML = "The value of Number Array is: " + numberArray;

// Show the Length of the Number Array
document.getElementById("numberArrayLength").innerHTML = "The length of Number Array is: " + numberArray.length;

// Show the First Element of the Number Array
document.getElementById("numberArrayFirstElement").innerHTML = "The first Element of Number Array is: " + numberArray[0];

convert the Number String to Number Array

I hope this code helped you to convert the Number string to Number Array.

Please like and share your valuable feedback on this article.