Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is the proper operator for “not equals” in JavaScript?

Understanding the Concept of Equality in JavaScript

Before we delve into the concept of "not equals" in JavaScript, it's crucial to first understand what we mean by equality. In the simplest terms, equality in JavaScript, or any other programming language, refers to the comparison between two values to check if they are the same. In JavaScript, we have two main equality comparison operators, == (loose equality) and === (strict equality).

Let's say we have two variables, x and y. If we were to write x == y or x === y, we are essentially asking the program, "Hey, are x and y the same?"

To demonstrate this, let's consider this piece of code:

let x = 10;
let y = '10';

console.log(x == y);  // Output: true
console.log(x === y); // Output: false

Here, x == y returns true because it only compares the values and not the type of the variables. On the other hand, x === y returns false because, in addition to comparing the values, it also compares the type of the variables.

The Not Equals Operator in JavaScript

Now that we've got that out of the way, let's talk about the "not equals" operator. Just as we have two types of equality operators, we also have two types of "not equals" operators in JavaScript: != (loose inequality) and !== (strict inequality).

The != operator checks if two values are not equal, disregarding their type. On the other hand, the !== operator checks if two values are not equal, or if they are not of the same type.

To make this clearer, let's look at an example:

let x = 10;
let y = '10';

console.log(x != y);  // Output: false
console.log(x !== y); // Output: true

In this case, x != y returns false because it only checks the values, and here the value 10 is equal to '10'. However, x !== y returns true, because it checks both the value and the type, and while the values are equal, the types (Number and String) are not.

Think of It Like Different Types of Fruit

To give you an analogy, think of JavaScript values as different types of fruit. The == operator just checks if you have a fruit, regardless of its type. So, if you have an apple and a picture of an apple, == would say they are the same.

On the other hand, === checks both the fruit and its type. If you compare an apple and a picture of an apple, === would declare that they are not the same because one is a real fruit and the other is a picture.

The != and !== operators work similarly. != only checks if the fruits are not the same, disregarding the type. So, if you have an apple and a picture of an apple, != would say these are the same. But !==, which checks both the fruit and its type, would declare that an apple and a picture of an apple are not the same.

Conclusion

Understanding the "not equals" operator in JavaScript is like learning a new language. At first, it may seem confusing, but with practice, it becomes more familiar. Remember, JavaScript is just like any other language. It has its own grammar (syntax), vocabulary (commands and functions), and rules (conventions). Just as you would learn a new spoken language by first understanding its basic components, you can learn JavaScript by first mastering its fundamental concepts, such as the "not equals" operator.

So, the next time you're writing a JavaScript program and need to check if two values are not the same, remember the difference between != and !==. The former is like saying, "These two things are not the same fruit, but I don't care what type they are." The latter is like saying, "These two things are not the same fruit, and I also care about what type they are." Happy coding!