Type Conversion

Converting between data types.

Examples

String to Number

Explicitly converting a string to a number.

let x = "10";
let y = Number(x); 
console.log(typeof y); // number

Number to String

Explicitly converting a number to a string.

let x = 123;
let y = String(x);
console.log(typeof y); // string

Automatic Conversion

JavaScript automatically converts types in expressions.

let result = "5" + 2; // "52"
let diff = "5" - 2; // 3

Test Your Knowledge

JavaScript Quiz

No quiz available for this topic yet.