"==" vs "==="

"==" vs "==="

Β·

2 min read

"What's different, it's the same yes?? πŸ€”πŸ’­"

No learner, it's not the same as you think by just adding one more equal to sign (=).

FIRSTLY,

  • \== operator is equal to "Loose Equality"

  • \=== operator is equal to "Strict Equality"

Equality Operators in JavaScript: == vs === | Code Wala

\== Operator (Loose Equality)

let's see the example

const a = 5;
const b = "5";
console.log(a == b); //output: true

what are you trying to say Vatsal?
I am trying to say that if we have taken a variable with the name a and assigned a number as 5 and another variable with the name b and assigned with a string as 5 .

So in the 1st case, it will convert the number into a string. (Type Conversion)

so, Now if we compare two with (==) we will get true as an output.

There are some cool rules :

  • If either operand is a string, the other operand will be converted to a string.

  • If either operand is a number, the other operand will be converted to a number

  • If either operand is a boolean, it will be converted to a number (true becomes 1 and false becomes 0).

  • If one operand is an object and the other is a primitive value, the object will be converted to a primitive value before the comparison is made.

  • If one of the operands is null or undefined, the other must also be null or undefined to return true. Otherwise, it will return false.

\=== Operator (Strict Equality)

Let's see with an example

const a = 5;
const b = "5";
console.log(a === b); //output: false

So, here in === operator, there is no type conversion. And it's just checking the value.

i.e we have taken a variable with the name a and assigned a number as 5, and another variable with a name b and assigned a string as 5 and over here one is a number and another one is in string. Both things can't be equal and the output will be false.

You can follow me over hereπŸ“±:

Linkedin: https://www.linkedin.com/in/vatsal-singh-732a20191/

Β