Skip to main content
X

Explore your training options in 10 minutes

JavaScript Operators: Comparison and Equality

Christina Kopecky - January 04, 2021


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:

  1. 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
  1. 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
  1. 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
   	}
  1. 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.

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

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

JavaScript Switch Case: A Step-By-Step Guide

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

JavaScript Syntax: A Guide for Beginners

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.

What's Next?

Christina Kopecky

About the author: Christina is an experienced technical writer, covering topics as diverse as Java, SQL, Python, and web development. She earned her Master of Music in flute performance from the University of Kansas and a bachelor's degree in music with minors in French and mass communication from Southeast Missouri State. Prior to joining the Career Karma team in June 2020, Christina was a teaching assistant, team lead, and section lead at Lambda School, where she led student groups, performed code and project reviews, and debugged problems for students. Christina's technical content is featured frequently in publications like Codecademy, Repl.it, and Educative.

Skip to main content