We first encountered operators when we were children learning arithmetic in elementary school. Operators are the symbols in between two operands. In web development, we use operators to compare two values to determine if an expression is true or false. In this article, we’ll take a look at comparison (a.k.a relational) and equality operators – they are the two most common types of operators you will encounter in JavaScript.
Comparison Operators
There will be times in creating logic to solve problems that you will need to use comparison or relational operators to conditionally render something to the screen. Let’s take a look at the most common ones you will see in your code. Here is the object we will use for the next few operators:
- in – use the in operator to see if there is a property in your object:
const cityData = { city: "San Jose", state: "California", area: 181.36, land: 178.24, water: 3.12, urban: 342.27, metro: 2694.61, elevation: 82, population: 1021795, timezone: "Los_Angeles/Pacific", website: "www.sanjoseca.gov" } console.log("metro" in cityData); //true console.log("country" in cityData); //false
- instanceof – use the instanceof operator to ask if an object is an instance of a class or constructor function.
function Class(subject, teacher, numStudents) { this.subject = subject; this.teacher = teacher; this.numStudents = numStudents; } const classroom = new Class('Academic Geometry', 'Jane Doe', 24); console.log(classroom instanceof Class); // expected output: true console.log(classroom instanceof Object); // expected output: true
- Less than ( < ), Less than or equal to ( <= ) – A comparison operator that returns true in a conditional statement if operand A is less than operand B. We see it most commonly when we create a for loop:
for(let i = 1; i <= n; i++) { // code here } for(let i = 0; i < arr.length; i++) { // code here }
- Greater than ( > ), Greater than or equal to ( >= ) – A comparison operator that returns true in a conditional statement if operand A is greater than operand B. It’s used quite often when we are trying to find the maximum number in an array:
let maximum = -Infinity; for(let i = 0; i < arr.length; i++) { if(arr[i] >= maximum) { maximum = arr[i]; } }
Note: >= is not the same as =>. The latter is used for big arrow functions in ES6 JavaScript syntax.
Equality Operators
Similar to comparison operators, equality operators also evaluate to a Boolean value that declares whether or not an expression is true.
Equality ( == ) vs. Identity ( === ) – When we see equal signs ( = ) in JavaScript, one equal sign is an assignment operator and not what we were used to when we were in math class.
Two equal signs is strictly an equality operator. It only checks to see if the values are equal by attempting to convert the values to the other’s data type. This is called type coercion.
console.log(8 == '8'); //true
Similarly, if we see a bang/exclamation point along with one equal sign ( != ), known as the inequality operator, it compares two values to see if operands are not equal in number. It doesn’t check for type.
console.log(8 != '4'); //true
Conversely, the identity operator, three equal signs ( === ), checks for type and number when comparing the two values.
console.log(8 === '8'); //false console.log(8 === 8); //true
Just like the inequality operator, the nonidentity operator ( !== ) checks to see if the operands are unequal. In addition, it checks the type as well. If they are, the condition is true and will return true. If not, it will return false.
console.log(8 !== '8'); //true console.log(8 !== 8); //false
Final Thoughts
Comparison and equality operators are essential to constructing logic in programming. When we compare the left operand with the right operand, we use equality operators to see if the values are equal in type, not in type, in type and number, or not in type and number. In addition, we use the comparison operators to help with the logic that will render a user interface (UI). When you feel comfortable with these operators, check out logical operators, the ternary operator, and bitwise operators!
Ready For More Tutorials on JavaScript?
Check these out!
JavaScript Ternary Operator: A Step-By-Step Guide
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.