Loading....
save

Difference between '==' and '===' in JavaScript

clock icon

asked 2 months ago

message icon

2

eye icon

483

I know this is basic, but can someone explain the difference between loose equality and strict equality with examples?

2 Answers

== (Loose Equality)

  • Compares values only
  • Performs type coercion (converts operands to the same type before comparing)

Examples

15 == "5" // true (string "5" is converted to number 5)
20 == false // true (false becomes 0)
3null == undefined // true
15 == "5" // true (string "5" is converted to number 5)
20 == false // true (false becomes 0)
3null == undefined // true

=== (Strict Equality)

  • Compares both value and type
  • No type coercion

Examples

15 === "5" // false (number vs string)
20 === false // false (number vs boolean)
3null === undefined // false
15 === "5" // false (number vs string)
20 === false // false (number vs boolean)
3null === undefined // false

1

Please log in to answer this question

Top Questions