- Arithmetic operators
- Assignment operators
- Comparison operators
Let us take a look at each of these operators and see how it works.
Quick notice about the operators
Before we proceed further, it is helpful to know the terms like operand and operators so you can understand what we are talking about in this lesson.
First, take a look at this simple math operation:
2 + 2 = 4
2 are known as operands and they can be either any string or a number.
= are called operators and they can be specified symbols, like +, -, >, <, and so on.
4 is called the result which is the outcome of the operations from both operands and operators.
Now that we are clear, let’s go ahead and learn these operators.
// 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
/, 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
let a = 2; console.log(a); // 2
In the example above, the
= operator assigns the value of
2 to the variable
false based on whether the comparison is true or not.
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
Data values such as
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
+, 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.
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.
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
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.
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
let x = 8; let y = 7; console.log(x y);
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
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.
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.
Let’s start with a blank page. First, create two variables called
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
Make sure to hit the “RUN” button to run the program.