Video Lesson

Lesson Notes

Understanding Equality Operators in JavaScript

In this video, I explain the different types of equality operators in JavaScript and how to avoid unexpected bugs by using the right one.

Key Concepts Covered

1. Declaring a Variable

1const myVar = 10;

2. Using console.log() to Check Equality

  • Basic comparison using triple equals (===):
1console.log(myVar === 10); // true
  • Checks both value and type.

3. Inequality Operator

1console.log(myVar !== 10); // false
  • The !== operator means "not equal in value or type."

4. Double Equals (==) vs. Triple Equals (===)

1console.log(myVar == "10"); // true (because of type coercion) 2console.log(myVar === "10"); // false (type mismatch)
  • == performs type coercion (tries to convert the types to match).
  • === does strict comparison (value and type must match).

5. Weird Behavior with Boolean Comparisons

1console.log(true == 1); // true 2console.log(false == 0); // true 3 4console.log(true === 1); // false 5console.log(false === 0); // false
  • == coerces true to 1 and false to 0.
  • === keeps types intact and results in false.

6. Best Practices

  • Always use === and !== to avoid unexpected results from type coercion.
  • Avoid using == or != unless you explicitly want loose comparison (rare in practice).

Summary

  • == compares values only, and will try to convert types if they don’t match.
  • === compares both value and type, making it safer and more predictable.
  • To avoid bugs and confusion, always use === and !== unless you have a very specific reason not to.

Example to test your understanding:

1const value = "1"; 2console.log(value == 1); // true (coerced) 3console.log(value === 1); // false (string vs number)

AI Assistant

Sign In Required

Sign in and subscribe to use the AI assistant for instant help with your lessons.

Sign In

Equality Operators in JavaScript

In this video, I explain how equality works in JavaScript. You'll learn the difference between == and ===, why type coercion can lead to unexpected results, and why it's best to use strict equality. Perfect for avoiding common beginner mistakes with comparisons in JavaScript.

5m 45s

Course Content

0 of 32 lessons completed
6. Promises, Async and Await
0 of 0 completed
7. The DOM
0 of 0 completed