Equality Operators in JavaScript

Equality and Type Conversion

There are two what I will call classes of operators for comparing in JavaScript. Strict and non-strict. I want to show you why if you have not already read some of my previous posts on object comparison, why you ought to use the strict comparison operators in just about all instances.

What Does Non-Strict Comparison Mean?

If you come from another programming language, you are already familiar with comparison operators. == and != should come to mind. In JavaScript, this is a non-strict comparison and the JavaScript runtime does something call type conversion, which was something foreign to me the first time I ran across it. When using == if variables are two different types the compiler will attempt to convert them to the same type for comparison of their primitive types. So for the example below the compiler will attempt to compare the values based on their converted types. Coming from most other programming languages you would expect this to fall into our else block of code. Not the case with JavaScript.

let one = 1;
let oneString = "1";
let oneBool = true; 

if(one == oneString == oneBool){
  console.log("The same"); //This is what the code block that will be executed.
}else{
  console.log("Not the same"); 
}

If we were to change this same piece of code and instead use the strict operator we would get our expected result. The == operator has changed to ===, and we will now get our expected result.

let one = 1;
let oneString = "1";
let oneBool = true; 

if(one === oneString === oneBool){
if(one == oneString == oneBool){
  console.log("The same"); 
}else{
  console.log("Not the same"); //This is what the code block that will be executed.
}

Do You Even Exist?

Another gotcha that is a part of the JavaScript language is when we check for the existence of a variable. This is a common practice in just about all languages and many other languages it is simple enough to use a simple check for the presence of our variable. Due to the nature of JavaScript at runtime and the implications of type conversion, we will run into similar issues when checking for the existence of a variable. Due to the nature of JavaScript runtime, we need to be careful of how we are checking if a value exists.

//Checking for existence of a variable
let val = 0; //as a bool would equate to false

if(val){ //actually converts to val == true due to runtime type conversion.
  console.log("exists");
}else{
  console.log("does not exist");
}

How do we check for this then? We need to use the typeof operator.

//Checking for existence of a variable
let val = 0; 

if(typeof val !== undefined){ //use the typeof operator to check if our variable has been defined or not.
  console.log("exists");
}else{
  console.log("does not exist");
}

Best practice for equality.

Use the === or !== operator to avoid type conversions. And if you are checking if a variable exists, use the above typeof operator for evaluation. There are other implications but these scenarios cover the majority of the edge cases that you’ll run into when writing JavaScript.

I hope you found this useful and as always Happy Coding!

Next On FoC

Dotnet Core Setup

Dotnet Core Setup
Previously On FoC

JavaScript Read-Only Properties and “use strict”;

JavaScript Read-Only Properties and “use strict”;