xxxxxxxxxx
const three = "3";
typeof(three);
// 'string'
const threeInt = parseInt(three, 10);
typeof(threeInt);
// 'number'
xxxxxxxxxx
var myInt = parseInt("10.256"); //10
var myFloat = parseFloat("10.256"); //10.256
xxxxxxxxxx
let num = Number("123"); // 123
let num = Number("123.45"); // 123.45
let num = Number("blah"); // NaN (not a number)
xxxxxxxxxx
var myString = "869.99"
var myFloat = parseFloat(myString)
var myInt = parseInt(myString)
xxxxxxxxxx
// Converting a String into a Number in Javascript
// Longhand:
const num1 = parseInt("100");
const num2 = parseFloat("100.01");
console.log(num1);
console.log(num2);
// Shorthand:
const num3 = +"100"; // converts to int data type
const num4 = +"100.01"; // converts to float data type
console.log(num3);
console.log(num4);
xxxxxxxxxx
// String to Int
myStringInt = "10";
console.log(parseInt(myStringInt)); // expected result: 10
// String to Float
myStringFloat = "8.33";
console.log(parseFloat(myStringFloat)); // expected result: 8.33
// String to any numeric type
console.log(Number(myStringInt)); // expected result: 10
console.log(Number(myStringFloat)); // expected result: 8.33
xxxxxxxxxx
const stringNumber = "123"; // Example string number
const number = Number(stringNumber);
console.log(number); // Output: 123
console.log(typeof number); // Output: number