Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is not equal in JavaScript

Understanding the Concept of Equality

When you're just starting out in the world of programming, there are concepts that might seem a bit confusing. One of these could be the principle of 'equality' in JavaScript. When we say 'equality', we don't mean it in the mathematical sense. Instead, we're referring to how JavaScript compares two values to see if they're the same or not.

For this discussion, we'll focus on the concept of 'not equal' in JavaScript. To understand this, think of it like comparing apples and oranges. Sure, they're both fruits, but they're not the same type of fruit, right?

JavaScript's Way of Saying 'Not the Same'

In JavaScript, the way to express 'not equal' is by using the symbol !=. This is a combination of the 'not' symbol (!) and the 'equal to' symbol (=). Put them together, and you've got 'not equal to'.

Here's a simple example:

let apple = "Apple";
let orange = "Orange";

console.log(apple != orange);  // Output: true

In this case, "Apple" is not equal to "Orange", so the result is true.

The Twist: Loose Inequality vs. Strict Inequality

Just when you thought you've got the hang of it, JavaScript throws in a twist: there's actually two types of 'not equal' in JavaScript – loose inequality (!=) and strict inequality (!==).

So what's the difference?

Loose Inequality (!=)

When JavaScript encounters the loose inequality operator (!=), it first tries to convert the values to a common type before making the comparison. This can lead to some unexpected results if you're not careful.

For example:

let number = 7;
let string = "7";

console.log(number != string);  // Output: false

Even though one is a number and the other is a string, JavaScript converts the string "7" to a number 7, sees that they're the same, and thus returns false.

Strict Inequality (!==)

Strict inequality (!==), on the other hand, doesn't do any type conversion. It simply compares the values and their types. If either the value or the type is different, then it's considered not equal.

Let's go back to our previous example:

let number = 7;
let string = "7";

console.log(number !== string);  // Output: true

In this case, even though the value 7 and "7" are the same, the types are different (one is a number, the other is a string). So JavaScript says they're not equal.

Understanding the Difference Through Metaphors

Think of the loose inequality (!=) as a casual friend who's not really picky. They won't mind if you show up in jeans or a suit, as long as you're there. On the other hand, strict inequality (!==) is like a strict parent. They care not only about the fact that you're home by curfew, but also that you've done your chores, finished your homework, and are in bed on time.

When to Use Which

As a rule of thumb, use loose inequality when you don't care about the type and strict inequality when you do. Your choice will depend on what you're trying to achieve in your code.

Conclusion: Not All Inequalities are Created Equal

Hopefully, you now have a better understanding of 'not equal' in JavaScript. Whether it's loose or strict, each has its place and purpose. So the next time you see != or !==, you can confidently say, "Ah, I know what you're about!"

Remember, coding is like a journey. Each step, no matter how small, takes you closer to your destination. The key is to keep moving forward, keep learning, and most importantly, keep coding. Happy coding!