JS Guide
HomeQuestionsSearchResources
Search

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext
javascript
junior
operators

What is the difference between == and === in JavaScript?

operators
equality
type-coercion
fundamentals
Quick Answer

== performs type coercion before comparison (loose equality), while === compares both value and type without coercion (strict equality).

Detailed Explanation

JavaScript has two equality operators:

== (Loose Equality)

  • Performs type coercion if operands are different types
  • Converts operands to a common type before comparison
  • Can lead to unexpected results
  • Generally discouraged in modern JavaScript

=== (Strict Equality)

  • No type coercion
  • Returns true only if both value AND type are the same
  • More predictable behavior
  • Recommended for most comparisons

Best Practice: Always use === unless you specifically need type coercion.

Code Examples

Loose vs Strict equality
// Loose equality (==) - type coercion happens
5 == '5'       // true (string '5' converted to number)
0 == false     // true (false converted to 0)
'' == false    // true (both convert to 0)
null == undefined // true (special case)

// Strict equality (===) - no type coercion
5 === '5'      // false (different types)
0 === false    // false (different types)
'' === false   // false (different types)
null === undefined // false (different types)

Real-World Applications

Use Cases

Null/Undefined Checking

Using value == null to catch both null and undefined in a single check (acceptable use of ==)

Form Validation

Strict comparison ensures type safety when validating user input against expected values

API Response Validation

Using strict equality to verify response data types match expected schema

Mini Projects

Equality Table Visualizer

beginner

Build an interactive tool showing type coercion results for all type combinations

Type Coercion Quiz

intermediate

Create a game testing knowledge of == vs === behavior with edge cases

Industry Examples

ESLint

The eqeqeq rule enforces === and !== usage with an option to allow == null

TypeScript

Strict mode catches type mismatches at compile time that == would silently coerce at runtime

jQuery

Historically used == for null checks; modern JavaScript codebases have shifted to ===

Resources

MDN - Equality comparisons

docs

JavaScript Equality Table

article
Previous
What is the difference between map(), filter(), and reduce() array methods?
Next
What is the difference between arrow functions and regular functions?