toNumber

by Berat Emre NebioÄźlu

JavaScript

console.log(Number('1'));
console.log(Number(false));
console.log(Number(null));
console.log(Number(undefined));

console.log(parseInt('1'));
console.log(parseInt(false));
console.log(parseInt(null));
console.log(parseInt(undefined));

// both do explicit type casting
// But check out the result of the experssions below

console.log('number: ' +  Number('123dsf'));
console.log('parseInt: ' +  parseInt('123dsf'));

//As you see below, even thoguh both function are doing same thing, parseInt function parses the input and able to understand the typeof character in the string. Whereas number is not able to do that.