DEV Community

Cover image for Understanding =, ==, and === in Programming
Dilan
Dilan

Posted on

Understanding =, ==, and === in Programming

When learning to code, it’s important to understand how assignments and comparisons work. In JavaScript (and many other programming languages), =, ==, and === have different purposes. Let’s break them down step by step with simple explanations and examples.

1. Assignment Operator =

The = symbol is used to assign a value to a variable. It does not compare values; it simply stores a value in a variable.

Example:

let x = 5; // Assigns the value 5 to the variable x
console.log(x); // Output: 5
Enter fullscreen mode Exit fullscreen mode

Here, x = 5 means "store the value 5 in the variable x." It is not a comparison.

2. Loose Equality ==

The == operator checks if two values are equal after converting them to the same type (type coercion). This means it tries to make the values the same type before comparing them.

Examples:

console.log(5 == '5'); // true (string '5' is converted to number 5)
console.log(0 == false); // true (false is converted to number 0)
console.log(null == undefined); // true (special case: null and undefined are loosely equal)
Enter fullscreen mode Exit fullscreen mode

While == is useful in some cases, it can lead to unexpected results because of type coercion.

3. Strict Equality ===

The === operator checks if two values are equal without converting their types. It compares both the value and the type.

Examples:

console.log(5 === '5'); // false (number and string are different types)
console.log(0 === false); // false (number and boolean are different types)
console.log(null === undefined); // false (different types: null and undefined)
Enter fullscreen mode Exit fullscreen mode

Strict equality is more predictable because it avoids type coercion. Most programmers prefer === for comparisons to prevent unexpected bugs.

Key Differences

Common Mistakes to Avoid

  1. Using = instead of == or ===:
let a = 10;
if (a = 5) { // This assigns 5 to a, instead of comparing
    console.log('This will always run');
}
Enter fullscreen mode Exit fullscreen mode
  1. Fix it by using == or === for comparisons:
if (a === 5) {
    console.log('This will run only if a is 5');
}
Enter fullscreen mode Exit fullscreen mode
  1. Relying on == with different types:
console.log('' == false); // true (type coercion)
console.log('' === false); // false (different types)
Using === prevents unexpected behavior caused by type coercion.
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Use = for assignments only.
  • Use === for comparisons unless you specifically want to allow type coercion.
  • Avoid using == unless you’re sure of what you’re doing and understand how type coercion works.

Conclusion

Understanding the difference between =, ==, and === is crucial for writing clean and bug-free code. Remember:

= assigns values.
== compares values with type coercion.
=== compares values without type coercion (strict equality).
Stick with === for most comparisons to ensure your code behaves as expected. Happy coding!

Top comments (0)