Type Conversion
Number("42") converts text to a number, String(42) converts to text and Boolean(value) converts to true or false. parseInt and parseFloat read numbers from the start of a string.
JavaScript also converts automatically ("5" + 1 is "51" but "5" - 1 is 4). Convert on purpose so results are predictable.
Example
console.log(Number("42") + 1);
console.log("5" + 1);
console.log("5" - 1);
console.log(parseInt("12px"));
console.log(Number("abc"));
console.log(Boolean(""), Boolean("hi"));Exercise
Convert the text to a number.
const n = ("42");
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.