Back to JavaScript Fundamentals

What is the difference between double equals and triple equals in JavaScript?

Understanding the difference between double (loose) and triple (strict) equals in JavaScript is crucial for writing correct and efficient code. Let's dive into the details and learn how to use them effectively.
4 min readLogan FordLogan Ford

Understanding JavaScript Equality Operators: == vs ===

JavaScript provides two ways to compare values for equality: the loose equality operator (==) and the strict equality operator (===). While they may seem similar, understanding their differences is crucial for writing reliable code.


Loose Equality Comparison (==)

The loose equality operator performs type coercion before comparing values. This means it will attempt to convert operands to the same type before making the comparison.


How Loose Equality Works

1// Type coercion examples
2console.log(1 == '1'); // true - string '1' is converted to number 1
3console.log(true == 1); // true - true is converted to number 1
4console.log(false == 0); // true - false is converted to number 0
5console.log(null == undefined); // true - special case in JavaScript

While loose equality can be convenient, it often leads to unexpected behavior and is generally discouraged in modern JavaScript development.


Strict Equality Comparison (===)

The strict equality operator compares both value and type, without performing type coercion. This makes it more predictable and safer to use.


How Strict Equality Works

1// No type coercion
2console.log(1 === '1'); // false - different types
3console.log(true === 1); // false - different types
4console.log(null === undefined); // false - different types

When to Use Each Operator

Use Strict Equality (===) when:

  • Writing production code
  • You need precise type checking
  • You want to avoid unexpected type coercion
  • Following modern JavaScript best practices

Use Loose Equality (==) when:

  • You specifically need type coercion
  • Working with legacy code that relies on loose equality
  • You're absolutely certain about the types you're comparing

Common Pitfalls with Loose Equality

1// Unexpected results with loose equality
2console.log([] == false); // true
3console.log('' == 0); // true
4console.log([1,2] == '1,2'); // true
5console.log(null == 0); // false
6console.log('' == '0'); // false
7console.log(0 == ''); // true

These examples show why loose equality can be dangerous - it's not always intuitive how JavaScript will coerce types.


Best Practices for Equality Comparisons

  1. Use strict equality (===) by default
  2. Only use loose equality (==) when you have a specific reason
  3. Be explicit about type conversions when needed
  4. Consider using type checking functions for more complex comparisons
1// Good practices
2// Explicit type conversion when needed
3const userInput = '42';
4const number = Number(userInput);
5console.log(number === 42); // true
6
7// Type checking for more complex cases
8console.log(typeof userInput === 'string'); // true
9console.log(Array.isArray([])); // true

Practice Exercises

Ready to test your understanding of JavaScript equality operators? Try these exercises to reinforce your knowledge!

Accelerate Your JavaScript Journey

TechBlitz offers comprehensive JavaScript training with hands-on exercises, real-world projects, and expert guidance. Whether you're just starting out or looking to deepen your understanding of JavaScript concepts, our platform provides the resources you need to succeed. Join our community of developers and take your programming skills to the next level!

Learn to code, faster

Join 810+ developers who are accelerating their coding skills with TechBlitz.

Share this article