Tuesday, January 28, 2020

whats the difference between == and === ?

The difference between == and === is that:


1. == converts the variable values to the same type before performing comparison. This is called type coercion.
2. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

Example:

  1. var one = 1;
  2. var one_again = 1;
  3. var one_string = "1"; // note: this is string
  4. console.log(one == one_again); // true
  5. console.log(one === one_again); // true
  6. console.log(one == one_string); // true. See below for explanation.
  7. console.log(one === one_string); // false. See below for explanation

No comments: