1.

What are logical errors in JavaScript?

Answer»

 JavaScript has both strict and type-converting EQUALITY comparison. For strict comparison we use === and for type-converting comparison we use == . Let’s look at each of it in details :

Strict Comparison(===)
For strict comparison the items been compared must be the same type.

  • Two strings are strictly equal when they have the same sequence of characters, same LENGTH, and same characters in corresponding positions. 
console.log("Coder" === "Coder"); //true console.log("Coder" === "coder"); //false
  • Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another.
console.log(23 === 23); //true console.log(NaN === NaN); //false console.log(0 === -0); //true 
  • Two Boolean operands are strictly equal if both are true or both are false.
console.log(true === true); //true console.log(false === false); //true
  • Two objects are strictly equal if they refer to the same Object.
VAR obj = {}; var newObj = obj; console.log(newObj === obj); //true
  • Null and Undefined types are not equal
console.log(null === undefined); //false console.log(null == undefined); //true

Type-converting comparison
The == does a type conversion before comparing, if both items are of DIFFERENT types. 

As you can see in the below example. String 1 is equal to numeric 1, when USING ==. It is because the type of String 1 is converted to Number before comparison. 
It is the similar case with “1 == true”. The 1 is converted into Boolean, which is true before comparison.
Same expressions are not true while using ===, as we know it doesn’t do any type conversion.

console.log('1' == 1); //true console.log(1 == true); //true console.log(1 === true); //false console.log('1' === 1); //false


Discussion

No Comment Found