Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is != in JavaScript

Understanding the != Operator in JavaScript

When you're learning to code, you'll encounter a variety of symbols that may seem daunting at first. However, once you understand what they represent, they become powerful tools in your programming arsenal. Today, we'll be focusing on the != operator in JavaScript.

A Basic Introduction to JavaScript Operators

Before we dive into the != operator, let's quickly touch on what an operator is. In JavaScript, an operator is a symbol that tells the computer to perform specific mathematical or logical manipulations. JavaScript has a wide range of operators, and != is one of them.

Decoding != Operator

The != operator is a comparison operator, meaning it compares the values of two elements. The != symbol stands for 'not equal to'. When used, it checks whether the two values on either side of it are not equal. If they are not equal, it returns true; if they are equal, it returns false.

Let's break it down with an example. If we have the code 5 != 3, JavaScript will check if 5 is not equal to 3. Since 5 and 3 are indeed not equal, the expression would return true.

console.log(5 != 3); // Outputs: true

Understanding the Difference Between != and ==

If != checks for inequality, then its counterpart == checks for equality. It's like two sides of a coin. If 5 == 3 returned false because 5 is not equal to 3, then 5 != 3 would return true for the same reason.

console.log(5 == 3); // Outputs: false
console.log(5 != 3); // Outputs: true

The Twist: Loose Equality and Type Coercion

Now, here comes a twist. JavaScript is a dynamically typed language, which means a variable can hold different types of values. When comparing two values of different types, JavaScript performs an operation called 'Type Coercion'. This means it automatically converts one value type to another to make the comparison possible.

With !=, JavaScript performs loose equality checking, which means it first does type coercion to make the data types the same before making the comparison.

Let's see an example.

console.log('5' != 5); // Outputs: false

Even though one is a string and the other is a number, JavaScript converts the string '5' to a number 5, realizes they are equal, and hence returns false.

The !== Operator: Strict Inequality

If you want JavaScript to compare the values and their types without performing type coercion, you would use the !== operator, also known as the strict inequality operator.

console.log('5' !== 5); // Outputs: true

In this case, although the values are the same, the types are different (string vs. number), hence it returns true.

Conclusion: The Power of !=

Learning to code can sometimes feel like you're learning a new language, with its own syntax and grammar. The != operator is one of the grammatical structures of JavaScript, a symbol that tells a story of comparison and difference.

Just as knowing more words in a language can make you a better speaker, understanding more operators in JavaScript can make you a better programmer. As you continue your coding journey, remember that each operator, each symbol, each line of code is a stepping stone to creating something amazing. So, embrace the != and the countless other operators. They're not just symbols; they're your building blocks to becoming a proficient JavaScript developer.