Basic Arithmetic Operations In JavaScript

Basic Arithmetic Operations In JavaScript

In JavaScript, there are some interesting basic arithmetic that can be done with arithmetic operators. In today's article, from my new series, Get Started with JavaScript, I'll explain how to perform basic Math operations in JavaScript.

If you're new to this topic in JavaScript, sit back and enjoy your ride as we dive in. Also, use your console to try out the examples.

In the previous article in this series, I explained all about variables and comments in JavaScript. Check it out so that you can follow along.

Contents


Basic Arithmetic Operations

Just like there are operators in Mathematics that can manipulate numbers(operands) to produce a certain result, so also in JavaScript, there are operators that manipulate numbers which are stored in variables to return a certain output. Basic arithmetic in Math is not different from basic arithmetic in JavaScript. For instance, in Math, we can add, subtract, multiply, and divide numbers, so also with JavaScript.

An operand is the data that is manipulated. It can be of any data type. For example in var y = 10 + 5, 10 and 5 are the operands while the + and = signs are the operators.

Let's take a look at the basic arithmetic operators in JavaScript.

Basic Arithmetic Operators

Addition: Two numbers can be added by placing the plus (+) symbol between the numbers.

Number is a data type in JavaScript that represents numerical data.

Take a look at this example:

//Adding two numbers 
var x = 10; 
var y = 20;
var sum =  x + y;  // returns 30

Subtraction: Similar to addition, two numbers can be subtracted by placing the minus (-) sign between them.

//Subtracting two numbers
var x = 100;
var y = 50;
var difference = x - y;  //returns 50

Division: You can divide the left number by the right by placing the division sign / between the numbers.

//Dividing two numbers
var x = 50;
var y = 25;
var answer = x / y;   //returns 2

Multiplication: Simply multiplies two numbers using the multiplication sign (*).

//Multiplying two numbers
var x = 10;
var y = 2;
var product = x * y;  //returns 20

Apart from these basic arithmetic operations mentioned above, there are other arithmetic operations peculiar to JavaScript that you may not find in Math. They are:
The increment (++) and Decrement (--) operators:The increment and decrement operators are used to add and subtract one to and from a variable.
i++ is the equivalent of i = i + 1 and i-- is the equivalent of i = i - 1. Check out this example

//using the increment operator
var num = 5;
num++;
/* returns 6 because you just added one to 5,
 how come?
num = num + 1;
*/


/* Using the decrement operator */
var y = 10;--
y--;
/*returns 9
y = y - 1;
*/

There's one more basic operator I would like to explain. It is the remainder operator (%) (commonly called modulus). It gives the remainder of the division of two numbers.

5 % 2 = 1
var remainder = 15 % 4;

blg-2.png


Assignment Operators

In my previous article, I covered how the basic assignment operator (=) is used. Today let's move on to more complex assignments. These operators provide useful shortcuts to keep your code neat and more efficient.

The Addition Assignment Operator (+=)

This simply adds a number to a variable, using the addition and the assignment symbols (+=). x = x + y => x += y.

//Using the addition assignment
var Aug = 5;
Aug += 10;

//The above is the same thing as
var Aug = 5;
Aug = Aug + 10;

console4.png

The Subtraction Assignment Operator (-=)

Similar to the addition assignment, this operator subtracts a number from a variable. x = x - y => x -= y

//Using the subtraction assignment operator
var x = 20;
x -= 10;

//The above is the same thing as 
var x = 20;
x = x - 10;

console5.png

The Multiplication Assignment Operator (*=)

This operator multiplies the value of a variable by a number. x = x * y => x *= y

//Using the multiplication assignment operator
var x = 20;
x *= 2;
// x will be multiplied by 2

console6.png

The Division Assignment operator (/=)

Like the others, this operator divides the value of a variable by a number. x = x / y => x /= y

//Using the division assignment operator
var x = 100;
x /= 10;
//divides 100 by 10

console7.png

The Remainder Assignment operator(%=)

This operator returns the remainder from dividing its first value/operand by its second value. x = x % y => x %= y

//Using the remainder assignment operator
var x = 20;
x %= 5;
//returns 0

console8.png


Summary

  • Basic arithmetic operators in JavaScript have the same usage as in math. They are:-
    Addition Operator Subtraction Operator Multiplication Operator Division Operator
    + - * /
  • Apart from the basic assignment operator (=), there are other compound assignment operators. They are :-
    Addition Assignment operator Subtraction Assignment Operator Multiplication Assignment Operator Division Assignment Operator
    += -= *= /=

Resources