JavaScript Loose vs Strict Equality

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional Modern JavaScript Lessons

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36

JavaScript Loose vs Strict Equality

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Loose Equality in JavaScript

console.log(0 == 0); // true βœ…
console.log('Hi' == 'Hi'); // true βœ…
console.log(456 == 456); // true βœ…

console.log(0 != 456); // true βœ…
console.log('Hi' != 'Bye'); // true βœ…

console.log(42 == '42'); // true βœ…

console.log(0 == false); // true βœ…
console.log('' == false); // true βœ…

console.log(5 == false); // false ❌
console.log('j' == false); // false ❌

Loose Equality is Unreliable

// Groundwork
console.log(0 == false); // true βœ…

// What we expect
// console.log(a == b); // true βœ…
// console.log(a == c); // true βœ…
// console.log(b == c); // true βœ…

// What we get
console.log('' == false); // true βœ…
console.log('0' == false); // true βœ… (∡ 0 == false)
console.log('' == '0'); // false ❌

Strict Equality

// === and !==

console.log(42 === '42'); // false ❌
console.log(42 === 42); // true βœ…
console.log(42 === +'42'); // true βœ…
console.log('message' !== 'hello'); // true βœ…
console.log(42 !== '42'); // true βœ…
javascript
typescript
react
playwright

Enjoy free content straight from your inbox πŸ’Œ

No spam, unsubscribe at any time.

Transcript

00:00

We've been using strict equality, which is triple equals throughout this course. But let's take a moment to discuss what is low equality or the double equals and why you should steer clear of it as much as possible. When the two data types on the boundary equality operator are the same, it doesn't matter if you use double equals or triple equals because the results will be the same. So as you would expect, a zero will be equal to zero, high will be equal to high, and 4, 5 6 will be equal to 4, 5, 6. And the same is true for lose inequality as well. So not equal behaves the same as not equal, equal if the data types are same on both sides.

00:34

So zero is not equal to 4, 5, 6 and high is not equal to buy. When the data types on the sites are different, then news equality does more. It tries to do type coercion as well. So even though the number 42 and the string 42 are not the same, lose equality will try to pass that string into a number and then do the comparison and therefore it it'll return true. It'll do that for various Boolean comparisons as well. For example, a zero is equal to false. Empty strings are equal to false. A positive number like five is not equal to false. And similarly,

01:08

a non empty string like J is not equal to false. And this type coercion is also run by the lose not equal operator. So the number 42 not equal to the string 42 will return false because once you do the passing on the string 42, they are equal and therefore not equal is false. From the examples, lose equality seems like it should be fine and mostly it is except that it breaks one critical rule of mathematics and that is the commutative property. So if a is equal to B and A is equal to C, then you would hope that B is equal to C.

01:41

So let's see if this is true for lose equality. Now, as you've seen before, zero is equal to false and similarly an MT string is equal to false. But what might catch you off card is there a string containing zero is actually going to be false as well. That is because that string is first going to get converted into a number and we've already established that zero is equal to false. And of course when types are the same, no type coercion takes place. So an empty string is a not equal to the string that contains zero. And this is what is breaking that original competitive property.

02:15

And this is not the only example that breaks this rule. So the question that you have to ask yourself, is it really worth it to be safe in avoiding silly mistakes which can be hard to debug. The guidance is to use only the strict inequality and strict equality operators and do any coercion that you want yourself. The strict equality and the strict inequality operators within JavaScript are triple equals and not equal equal. And I don't know about you, but I'm personally happy if my 42 number does not behave like a 42 string. And similarly of course 42 is equal to 42.

02:48

So if I really wanted to take that string and convert it into a number, I can do that myself. I can do 42 triple equals, convert that 42 into a number with a plus Operator and now they are equal. And similarly, the not equal equal operator does no type coercion. So just like message is not equal to hello, the number 42 is not equal to the string 42 because those equality type coercion has weird edge cases that no one working on modern JS needs to care about type state will actually mark type coercion loose equality as a compiled time error. Now, before I actually started recording this tutorial,

03:23

I actually commented out check js within my TSS config. This is because with this option enabled TypeScript will never allow me to write the crazy code that you have been seeing. And in fact, if you go back to that example where we proved that the double equals loose equality breaks the commutative property, it'll error with the message that this comparison appears. Unintentional, you are comparing strings and Boolean, don't mix your types 'cause you don't know what's going to happen. And even with the same types, it's going to complain that this is always going to be false. What do you think you are doing? I hope you enjoyed this tutorial on JavaScript equality. As always,

03:58

thank you for joining me and I will see you in the next one.

Professional Modern JavaScript

Professional Modern JavaScript

1.Course Intro
free
⏱️ 1:13
2.Setup & Tooling
free
⏱️ 3:32
3.Debugger Statements and Breakpoints
free
⏱️ 2:17
4.Primitive Data Types and Values
free
⏱️ 3:06
5.JavaScript Variables - let and const
free
⏱️ 4:50
6.Comments in JavaScript
free
⏱️ 2:55
7.JavaScript Identifer Naming
free
⏱️ 2:52
8.Using TypeScript
free
⏱️ 4:26
9.JavaScript String Masterclass
free
⏱️ 9:29
10.JavaScript Boolean Type
⏱️ 6:45
11.Missing Guide to JavaScript Numbers
⏱️ 15:28
12.JavaScript Objects Demystified
⏱️ 13:33
13.Functions in JavaScript
⏱️ 20:25
14.JavaScript Arrays Masterclass
⏱️ 22:31
15.JavaScript If Else Statements and Best Practices
⏱️ 6:13
16.JavaScript Conditional Expressions
⏱️ 8:38
17.JavaScript Loose vs Strict Equality
⏱️ 4:02
18.Truthy vs Falsy in JavaScript
⏱️ 3:44
19.Concept of JavaScript Nullish and Unification
⏱️ 5:51
20.JavaScript Classes - Object Oriented Programming
⏱️ 10:30
21.JavaScript Error Handling and Exceptions
⏱️ 13:21
22.JavaScript Nullish Operators
⏱️ 5:48
23.JavaScript Switch Statement Masterclass
⏱️ 8:07
24.JavaScript For Loop Iteration Simplified
⏱️ 10:59
25.JSON Masterclass
⏱️ 7:59
26.JavaScript While and Do While Loops
⏱️ 2:52
27.JavaScript Date and Time Simplified
⏱️ 13:16
28.this in JavaScript
⏱️ 12:04
29.JavaScript Tagged Templates Masterclass
⏱️ 5:48
30.Promises in JavaScript
⏱️ 10:01
31.JavaScript Async Await Masterclass
⏱️ 9:00
32.JavaScript Symbols Demystified
⏱️ 7:25
33.JavaScript Iterators and Iterables
⏱️ 8:50
34.JavaScript Generators Simplified
⏱️ 9:17
35.JavaScript Prototype - The Secret Guide
⏱️ 11:21
36.JavaScript Set Builtin Data Structure
⏱️ 9:52
37.JavaScript Map Builtin - HashMap Magic
⏱️ 10:38
38.JavaScript Deferred Promise Pattern - withResolvers
⏱️ 3:35
39.Cloning and Deep Copying in JavaScript
⏱️ 3:14
40.JavaScript Async Await Sequencing and Execution Masterclass
⏱️ 10:31
41.JavaScript Memory Management Masterclass
⏱️ 5:26
42.JavaScript WeakMap Demystified
⏱️ 8:58
43.JavaScript bigint Masterclass
⏱️ 5:35
44.JavaScript WeakSet Explained with Examples
⏱️ 7:47
45.JavaScript Regular Expressions Masterclass
⏱️ 17:54
46.JavaScript Weak References Demystified
⏱️ 5:29
47.JavaScript Memory Leaks Demonstrations and Fixes
⏱️ 6:01
48.Semicolon Free Coding in JavaScript
⏱️ 3:46
49.JavaScript Modules Masterclass
⏱️ 11:36