Arithmetic operators

In JavaScript, like other programming languages, we can do some arithmetic operations. In your school, you might have already learned how to add two numbers, subtract one number from another number, multiply two numbers, and divide a number with another. You can do all these things in JavaScript with the help of a few lines of code.

In JavaScript, we use the following arithmetic symbols for the operations:

Operator

Description

+

To add

-

To subtract

*

To multiply

/

To divide

%

To find the reminder (called modulus operator)

Addition

Suppose you have two variables, x and y, with the values 3 and 4, respectively. What should we do on the console to store the values on the variables?

Yes, we do the following:

var x = 3; // 3 is stored on variable x
var y = 4; // 4 is stored on variable y

Then, press Enter.

Take another variable that will hold the summation of x and y, as follows:

var z = x+y; // This syntax stores the sum of x and y on z

Can you tell me what will happen when we print z?

document.write(z);

Yes, you are correct, this will print 7, as shown in the following screenshot:

Addition

Subtraction

To subtract a number from another, you need to put a minus sign (-) between them.

Let's consider the following example:

var x = 9; // 9 is assigned to the variable x.
var y = 3; // 3 is assigned to the variable y.
var z = x - y ; // This syntax subtracts y from x and stores on z.
document.write(z); // Prints the value of z.

The output of this code is 6, as shown in the following screenshot:

Subtraction

Multiplication

To multiply two numbers or variables that have integer or float type of data stored on them, you just put an asterisk (*) between the variables or numbers.

Let's take a look at the following example:

var x = 6; // 6 is assigned to the variable x.
var y = 2; // 2 is assigned to the variable y.
var z = x * y; // For two numbers you can type z = 6 * 2 ;
document.write(z); // Prints the value of z

The output of this code is 12, as shown in the following screenshot:

Multiplication

Division

To divide a number with another, you need to put a forward slash (/) between the numbers.

Let's take a look at the following example:

var x = 14; // assigns 14 on variable x.
var y = 2; // assigns 2 on variable y. 
var z = x / y; // divides x with y and stores the value on z. 
document.write(z); // prints the value of z. 

The output of this code is 7, as shown in the following screenshot:

Division

Modulus

If you want to find the modulus of a number with another, you need to put a percentage sign (%) between the numbers.

Let's consider the following example:

var x = 34; // assigns 34 on the variable x. 
var y = 3; // assigns 3 on the variable y. 
var z = x % y ; // divides x with y and returns the reminder and stores on the variable z
document.write(z);

The output of this code is 1, as shown in the following screenshot:

Modulus

Tip

What does modulus (%) operator do?

Well, from your math class, you have already learned how to divide one number with another. Say, you divide 10 by 2. The result will be 5, which is an integer type of number. However, what will happen if you divide 10 by 3? The answer will not be an integer. The value is 3.333333333333. You can also say that the answer is 3 and the remainder is 1. Consider the following:

10 = 9 + 1;

That is, (9+1)/3

= 9/3+1/3

= 3 + 1/3;

Therefore, the remainder is 1. What modulus does is that it finds out the remainder and returns it. Therefore, 10%3 = 1.

Now, let's summarize all the arithmetic operators that we learned so far in one single code.

Can you tell me the output of the following lines?

var x = 5 ;
var y = 4 ;
var sum = x + y ;
var sub = x - y ;
var mul = x * y ;
var div = x / y ;
var mod = x % y ;
document.write("The summation of x and y is "+ sum + "<br>") ;
document.write("The subtraction of x and y is " + sub + "<br>") ;
document.write("The multiplication of x and y is " + mul + "<br>");
document.write("The division of x and y is " + div + "<br>") ;
document.write("The modulus of x and y is " + mod + "<br>") ;

You will get the following output:

The summation of x and y is 9

The subtraction of x and y is 1

The multiplication of x and y is 20

The division of x and y is 1.25

The modulus of x and y is 1

This output can be seen in the following screenshot:

Modulus

I guess you nailed it. Now, let's explain them in the following:

  • We assigned 5 and 4 to x and y, respectively
  • We assigned the summation of x and y to the sum variable, the subtraction of x and y to the sub variable, the multiplication of x and y to the mul variable, the division of x and y to the div variable, and the modulus of x and y to the mod variable
  • Then, we printed them using the document.write(); syntax
  • We used a <br> HTML tag to separate the output of each line

Consider the following example:

John has 56 pens. He wants to arrange them in seven rows. Each line will have an equal number of pens. Write a code that will print the number of pens in each row.

(Hint: take two variables for the number of pens and number of rows, divide the number of pens with the number of rows and store the value in a new variable.)

The sample output is as follows:

John will have to place XX pens on each line. // XX is the number of pens

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.249.210