Home > OS >  Why does == changes === verification? Javascript comparison Operator
Why does == changes === verification? Javascript comparison Operator

Time:12-16

when i set

let x = 5
let y = 10
let z = "5"

console.log(x >= y) //false, ok
console.log(x <= y) //true, ok
console.log(x=z) //5, ok
console.log(x==z) //true, ok
console.log(x===z) //true <----WHY?

Then when i delete some lines

let x = 5
let y = 10
let z = "5"

console.log(x >= y) //false, ok
console.log(x <= y) //true, ok
console.log(x===z) //false <----WHY?

changes from true to false, why are those console log changing the results

I just know it has to do with the == operator.Explanation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Strict_equality

CodePudding user response:

In your case, x = z is an assignment. Assignment operator assign value to other var.

  • == matches value
  • === matches (value type)

Example:

const x = "5"

(x=="5") => true
(x===5) => false (Because 5 is integer here)

CodePudding user response:

So thats because in your third console.log statement ,you are assigning the value rather than checking it, which actually gives the value of x to z ,so the condition becomes true, It has nothing major to do with the functionality of "===" operator.

  • Related