In JavaScript, type conversion refers to the process of converting a value from one data type to another. JavaScript performs type conversions automatically in many situations, but you can also perform type conversions explicitly.

  1. Implicit Type Conversion JavaScript often performs type conversion automatically when required. This is known as type coercion. Here are some common examples:

    • String Conversion:
      let num = 123;
    let str = num + ""; // Implicitly converts number to string
    console.log(str); // "123"
      
    • Number Conversion:
      let str = "456";
    let num = +str; // Implicitly converts string to number using the unary plus operator
    console.log(num); // 456
      
    • Boolean Conversion:
      let value = 0;
    if (value) {
        console.log("True");
    } else {
        console.log("False"); // 0 is converted to false
    }
      
  2. Explicit Type Conversion You can explicitly convert values to different types using built-in functions and operators.

    • To String:

      • String Constructor:

          let num = 123;
        let str = String(num);
        console.log(str); // "123"
          
      • String Method:

          let num = 123;
        let str = num.toString();
        console.log(str); // "123"
          
    • To Number:

      • Number Constructor:

          let str = "456";
        let num = Number(str);
        console.log(num); // 456
          
      • Unary Plus Operator:

          let str = "456";
        let num = +str;
        console.log(num); // 456
          
      • parseInt and parseFloat:

          let strInt = "123";
        let numInt = parseInt(strInt);
        console.log(numInt); // 123
        
        let strFloat = "123.45";
        let numFloat = parseFloat(strFloat);
        console.log(numFloat); // 123.45
          
    • To Boolean:

      • Boolean Constructor:

          let value = 1;
        let bool = Boolean(value);
        console.log(bool); // true
          
      • Double Negation Operator:

          let value = 0;
        let bool = !!value;
        console.log(bool); // false
          
  3. Special Cases

    • null and undefined:

      • null to Number:

          let value = null;
        let num = Number(value);
        console.log(num); // 0
          
      • undefined to Number:

          let value = undefined;
        let num = Number(value);
        console.log(num); // NaN
          
    • NaN:

        let value = NaN;
      let num = Number(value);
      console.log(num); // NaN
        

Examples

Here are a few examples demonstrating different type conversions:

  // String to Number
let str = "789";
let num = Number(str);
console.log(num); // 789

// Number to String
let num2 = 456;
let str2 = num2.toString();
console.log(str2); // "456"

// String to Boolean
let str3 = "hello";
let bool = Boolean(str3);
console.log(bool); // true (non-empty string is truthy)

// Boolean to Number
let bool2 = true;
let num3 = Number(bool2);
console.log(num3); // 1