You know how a calculator needs operators like add, subtract, multiply, and divide to do basic calculations, right? JavaScript also has operators, and we use them to perform basic programming operations.

Here are the common types of operators in JavaScript, and they are:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators

Let us take a look at each of these operators and see how it works.

Arithmetic operators

The Arithmetic operator is the type of JavaScript operator that does mathematical calculations. It will take numerical values that can be either literals or variables as their operands and returns a single numerical value.

For example:

// Additions
let a = 5;
let b = 3;

let sum = a + b;

console.log(sum); // 8

In the code above, variable a is added to variable b using the addition operator + As a result, it will display 8 because 5 plus 3 is equal to 8.

Besides the addition +, other standard arithmetic operators are subtraction -, multiplication *, division /, and remainder %.

More examples of Arithmetic operators

let a = 5;
let b = 2;

// Subtractions
let subtracts = a - b;

console.log(subtracts); // 3

// Multiplications
let multiply = a * b;

console.log(multiply); // 10

// Divisions
let divide = a / b;

console.log(divide); // 2.5

// Remainders
let remainder = a % b;

console.log(remainder); // 1

Assignment operators

The Assignment operator is another type of JavaScript operator that assigns the values to the JavaScript variables. While there are different kinds of assignment operators, the most frequently used is the equal = operator.

For example:

let a = 2;

console.log(a); // 2

In the example above, the = operator assigns the value of 2 to the variable a.

If this looks familiar, then you may have already learned it from the JavaScript variable lesson.

Comparison operators

The Comparison operator is the type of JavaScript operator that compares two values and returns the boolean value as true or false based on whether the comparison is true or not.

For example:

let a = 2;
let b = 2;

let check = a == b;

console.log(check); // true

In the example above, the “equal to” == operator checks if the values of variable a and variable b are the same. Because they match each other, the output will return as true. If it didn’t match, it will return false.

Data values such as true and false are called booleans, and it is a data type just like strings and numbers. The difference is it can only have two values: true and false. You will learn more about it in the next lesson.

Apart from the equal == operator +, other common comparison operators are not equal to !=, greater than >, and less than <.

More examples of comparison operators

let a = 5;
let b = 2;

// Not equal operator
let notEqual = a != b;

console.log(notEqual); // true

// Equal operator
let equal = a == b;

console.log(equal); // false

// Greater than operator
let greaterThan = a > b;

console.log(greaterThan); // true

// Less than operator
let lessThan = a < b;

console.log(lessThan); // false

The Comparison operator is especially useful when working with the if statement and loops. You will learn how comparison operators are used in-depth in later tutorials.

Coding tasks

Alright, it is time to put the JavaScript operators lessons into practice by solving the following tasks.

In the first task, you will write new code to get the desired output. For the next three tasks, you will have to change the existing code to fix the problem. For the fifth and final task, you will write a new code in the editor from scratch.

Task 1

We want you to create a simple arithmetic program that will add two numbers together and the output should be 100. For this task, you will use the addition + operator.

Type the following commands in the code editor:

let x = 63;
let y = 37;
let sum = a + b;

console.log(sum);

And press the “RUN” button to run the program.

Task 2

There is a missing arithmetic operator in this code. Add the correct operator inside the console log so that multiplying the x variable by the y variable will display 56.

let x = 8;
let y = 7;

console.log(x  y);

Task 3

A developer has made a bug in this code. It is supposed to check if the first name variable is the same as the second name variable, but instead, the result shows the string John and not boolean true.

let firstName = "John";
let secondName = "John";

console.log(firstName = secondName);

Help fix the bug so that the code will return true in the output.

Hint: Add an extra = symbol in the console log.

Task 4

There’s another mistake in this code. It is supposed to check if the value of the variable x is greater than variable y and return true. But instead, it is returning false because a wrong comparison operator has been used.

let x = 21;
let y = 18;

console.log(x < y);

Fix the mistake so that it returns true in the output.

Hint: Change one character in the console log.

Task 5

Let’s start with a blank page. First, create two variables called a and b. Then assign value 5 to both of the variables. Finally, add them together in the console log using the addition operator + so the result will be 10

Make sure to hit the “RUN” button to run the program.