That's a great question! While this step introduces the loose equality operator (==), in modern JavaScript development, it is generally recommended to use strict equality (===) most of the time.
Here is a quick guide on when and why to use it:
1. Use Strict Equality (===) by Default
You should use === whenever you want to ensure that both the value and the data type are exactly the same. It does not perform "type coercion" (automatic conversion).
- Example:
10 === "10" // false (One is a Number, one is a String) 10 === 10 // true
Using === helps prevent subtle bugs. For instance, false == 0 is true, but false === 0 is false. Strict equality makes your code more predictable.
2. When to use Loose Equality (==)
You might use == if you deliberately want to treat different types as the same value. The most common "pro" use case is checking if a value is either null or undefined at the same time:
- Example:
if (variable == null) { // This matches both null and undefined }
Summary
- Best Practice: Always use
===unless you have a specific reason to allow type conversion. - Why: It avoids unexpected results caused by JavaScript's automatic type conversion rules.
In the next steps of your learning journey, you will likely encounter the strict equality operator again! Feel free to keep experimenting with your script.js file.