javascript string to int conversion code example

Example 1: how to convert string to int js

let string = "1";
let num = parseInt(string);
//num will equal 1 as a int

Example 2: convert a string to number in javascript

var x = Number("1000")

Example 3: convert string to integer javascript

const stringPrices = ['5.47', '3.12', '8.00', '5.63', '10.70'];
let priceTotal = 0;

// priceTotal should be: 32.92
// Write your code below

stringPrices.forEach(stringPrice => {
price = parseFloat(stringPrice);
  priceTotal += price;
});

Example 4: convert a string to number in javascript

var x = parseInt("1000", 10); // you want to use radix 10
    // so you get a decimal number even with a leading 0 and an old browser ([IE8, Firefox 20, Chrome 22 and older][1])

Example 5: string to int javascript

// Convert strings
Number('123'); // returns 123
Number('12.3'); // returns 12.3
Number('3.14someRandomStuff'); // returns NaN
Number('42px'); // returns NaN

Example 6: js convert string to number

let string = "1"
console.log(+string)