string into number in javascript 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: Javascript string to int

var myInt = parseInt("10.256"); //10
var myFloat = parseFloat("10.256"); //10.256

Example 3: converst strig in number in js

const numberInString = "20"; 
console.log(typeof(numberInString)) // typeof is string this is string in double quote " "
const numInNum = parseInt(numberInString) // now numberInStrings variable converted in an Integer due to parseInt
console.log(typeof(numInNum)) // this tell us the type of numInNum which is now a number

Example 4: javascript string to integer

<script language="JavaScript" type="text/javascript"> 
var a = "3.3445";
var c = parseInt(a);
alert(c);
</script>

Example 5: javascript convert a number in string

const num = 123; //> type number 123
const str = num.toString(); //> type string "123"

Example 6: from string to number js

var text = '42px';
var integer = parseInt(text, 10);
// returns 42