JavaScript Type Conversion

Converting between different data types in JavaScript

🔄 What is Type Conversion?

Type conversion is the process of converting one data type to another. JavaScript can convert types automatically (implicit) or you can convert them manually (explicit).


// Automatic conversion
let result = "5" + 3; // "53" (string)

// Manual conversion
let number = Number("5"); // 5 (number)
                                    

Output:

result = "53" (string)

number = 5 (number)

Type Conversion Methods

🔢

To Number

Convert values to numbers

Number("123")    // 123
Number("12.34")  // 12.34
Number("")       // 0
Number("hello")  // NaN
📝

To String

Convert values to strings

String(123)      // "123"
String(true)     // "true"
String(null)     // "null"
(123).toString() // "123"

To Boolean

Convert values to true/false

Boolean(1)       // true
Boolean(0)       // false
Boolean("")      // false
Boolean("hello") // true
🔄

parseInt

Parse strings to integers

parseInt("123")    // 123
parseInt("123.45") // 123
parseInt("123abc") // 123
parseInt("abc123") // NaN

🔹 Converting to Numbers

There are several ways to convert values to numbers:

// Using Number() function
let str = "42";
let num1 = Number(str);        // 42

// Using parseInt() for integers
let num2 = parseInt("42.7");   // 42

// Using parseFloat() for decimals
let num3 = parseFloat("42.7"); // 42.7

// Using unary + operator
let num4 = +"42";              // 42

console.log(num1, num2, num3, num4);

Output:

42 42 42.7 42

🔹 Converting to Strings

Convert any value to a string representation:

// Using String() function
let num = 42;
let str1 = String(num);        // "42"

// Using toString() method
let str2 = num.toString();     // "42"

// Using template literals
let str3 = `${num}`;           // "42"

// Concatenating with empty string
let str4 = num + "";           // "42"

console.log(str1, str2, str3, str4);

Output:

"42" "42" "42" "42"

🔹 Converting to Booleans

Understanding truthy and falsy values:

// Falsy values (convert to false)
console.log(Boolean(0));        // false
console.log(Boolean(""));       // false
console.log(Boolean(null));     // false
console.log(Boolean(undefined)); // false
console.log(Boolean(NaN));      // false

// Truthy values (convert to true)
console.log(Boolean(1));        // true
console.log(Boolean("hello"));  // true
console.log(Boolean([]));       // true
console.log(Boolean({}));       // true

Output:

false false false false false

true true true true

🔹 Automatic Type Conversion

JavaScript automatically converts types in certain situations:

// String concatenation
let result1 = "5" + 3;      // "53" (number becomes string)
let result2 = "5" + true;   // "5true"

// Mathematical operations
let result3 = "5" - 3;      // 2 (string becomes number)
let result4 = "5" * "2";    // 10 (both become numbers)

// Comparison operations
let result5 = "5" == 5;     // true (loose equality)
let result6 = "5" === 5;    // false (strict equality)

console.log(result1, result2, result3, result4, result5, result6);

Output:

"53" "5true" 2 10 true false

🧠 Test Your Knowledge

What is the result of Number("123abc")?