4. Type Conversion and Comparison operators - Javascript

  1. Type Converison

    Type conversion (or typecasting) means the transfer of data from one data type to another

    We convert JS data type from one type to another :

    • By the use of a JavaScript function

    • Automatically by JavaScript itself

  1. By the use of JS functions (Explicit Conversion)

    1. Converting String to number : Number()

       console.log(Number("123")) // 123
       console.log(Number("")) // 0
       console.log(Number("Ayush")) // NaN
       console.log(Number(false)) // 0
      

2. Converting Numbers to Strings: String()

    console.log(String(123)) //123
    console.log((123).toString())//123
  1. Automatic type conversion(Type Coercion or Implicit Conversion)

    When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type.

    Type coercion is the automatic conversion of one data type into another by JavaScript.

console.log('5' + 2);  // "52" (string concatenation)
console.log('5' - 2);  // 3 (string '5' is converted to number)
console.log('5' * '2');  // 10 (both strings are converted to numbers)
console.log('5' / '2');  // 2.5 (both strings are converted to numbers)
console.log('5' + true);  // "5true" (true is converted to "true")
console.log(5 + false);  // 5 (false is converted to 0)
console.log(5 + null);  // 5 (null is converted to 0)
console.log(5 + undefined);  // NaN (undefined cannot be converted to number)

Comparison Operators

1. Loose Equality (==) : It compares two values after converting them to a common type.

2. Strict Equality (===) : It compares values as well as their data types**.**

3. Object.is() (Same Value Equality):

It is similar to === but with two differences:

  • Object.is(NaN, NaN) // true (Unlike ===, it considers NaN equal to itself)

  • Object.is(-0, 0) // false (Unlike ===, it distinguishes between -0 and 0)

      // == 
      console.log(5 == '5');  // true (string '5' is converted to number 5)
      console.log(0 == false); // true (false is converted to 0)
      console.log(null == undefined); // true (both are considered "empty" values)
      console.log('' == 0);  // true (empty string is converted to 0)
      console.log([] == 0);  // true ([] is converted to empty string "", then to 0)
    
      //===
      console.log(5 === '5');  // false (different types: number vs string)
      console.log(0 === false); // false (number vs boolean)
      console.log(null === undefined); // false (different types)
      console.log('' === 0);  // false (string vs number)
      console.log([] === 0);  // false (array vs number)
      console.log(NaN === NaN);  // false (NaN is never equal to itself)
    
      //Object.is()
      console.log(Object.is(5, 5));  // true
      console.log(Object.is(5, '5'));  // false
      console.log(Object.is(NaN, NaN));  // true (unlike ===)
      console.log(Object.is(-0, 0));  // false (distinguishes -0 and 0)
      console.log(Object.is([], []));  // false (different memory references)
    

Interview Questions

  1. What is the output of console.log([] == false);? Why?

    • Output: true

    • Explanation:

      • false is converted to 0

      • [] (empty array) is converted to an empty string ("")

      • "" == 0 results in true.

  2. What will be the output of console.log(5 + null);

    • Output: 5

    • Explanation:

      • null is converted to 0, so 5 + 0 = 5.

Next Topic :

4. Arrays and JSON