cast as int 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: convert string to number javascript

var myString = "869.99"
var myFloat = parseFloat(myString)
var myInt = parseInt(myString)

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: string to number js

let myNumber = Number("5.25"); //5.25

Example 5: string to number javascript

new Number(valeur);
var a = new Number('123'); // a === 123 donnera false
var b = Number('123'); // b === 123 donnera true
a instanceof Number; // donnera true
b instanceof Number; // donnera false

Tags:

Java Example