Convert Array of Strings to Array of Numbers in JavaScript-Code Examples

Victor Talmacinschi

Convert Array of Strings to Array of Numbers in JavaScript

JavaScript by Victor Talmacinschi June 21, 2022

const arrayOfStrings = ['1', '2', '3'];

// convert the strings in the array to numbers
const arrayOfNumbers = arrayOfStrings.map(string => Number(string));

console.log(arrayOfNumbers); // [1, 2, 3]

0

18.038

Convert Array of Strings to Array of Numbers in JavaScript

JavaScript by Victor Talmacinschi June 21, 2022

// convert an array of strings to an array of numbers
const arr = ['1', '2', '3'];

// declare a new empty array
const newArr = [];

// loop through arr
for (let i = 0; i < arr.length; i++) {
  // convert arr[i] to a number and push it into newArr
  newArr.push(Number(arr[i]));
}

// log newArr to the console
console.log(newArr);

0

18.038

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories