The arthemetic operators are those that use two or more operands in the form of constants or variables and return single value as results. The value of arithmetic expressions are stored in a variable or used in other expressions.
There are 6 arithmetic operators in JavaScript language. These are:
- Addition
- Subtraction
- Multiplication
- Division
- Remainder
- Exponential
We shall discuss each one of them with examples.
Addition Operator
The addition operator also known as plus(+), adds two numbers and output the result. The syntax for addition operator is given below.
//Syntax <variable> = <variable1> + <variable2> + ... + <variableN>;
Example #1
//variable declarations var A = 10; var B = 23; var C = A + B; //addition console.log(c);
Output #1
33
Subtraction Operator
Subtraction operator takes two operand and subtract the value of second operand from the first. The result is single value stored in a variable to display or used in a new expression. The subtraction is a left-to-right operation.
//Syntax <variable> = <variable1> - <variable2> - ... - <variableN>;
Example #2
//variable declaration var A = 20; var B = 12; var C = A - B; //subtraction console.log(C); //output
Output #2
8
Multiplication Operator
The multiplication operator takes two or more operands and multiply (*) them to get a single number result. The operands are constants or variables . Here is the syntax for multiplication.
//Syntax <variable> = <variable1> * <variable2> * ... * <variableN>;
Example #3
//variable declaration var A = 5; var B = 25; var C = A * B; //multiply console.log(C); //output
Output #3
125
Division Operator
Division operator or divide operation takes two operands and divide the first number with the second number. You get a result called the quotient of the division. The first number is called dividend and the second called divisor.
//Syntax <variable> = <variable1>/<variable2>;
Example #4
//variable declaration var A = 440; var B = 4; var C = A/B; //division console.log(C); //output
Output #4
110
Remainder Operator
Unlike division, the remainder operator will give remainder of a division , not quotient even if it is a zero. The is a special symbol to indicate remainder division (%).
//Syntax <variable> = <variable1> % <variable2>;
The second number divides the first number and give the remainder that gets stored in the variable to the left.
Example #5
//variable declaration var A = 18; var B = 4; var C = A % B; //remainder division console.log(C); //output
Output #5
2
Exponential Operator
JavaScript has a new operator called the exponential operator that takes two operands – number and its power. So, if x is the first and y is the second number, then x^y is represented by x ** y using exponential operator.
Example #6
//variable declaration var A = 6; var B = 2; var C = A ** B; // exponential operation console.log(C);//output
Output #6
36
In future post, we will discuss about more such operators.