Javascript Arithmetic Operators
Javascript Arithmetic Operators are used to perform arithmetic operations on variables.
Arithmetic Operators
Operator | Name | Description |
+ | Addition | Adds two values. |
- | Subtraction | Subtracts two values. |
* | Multiplication | Multiplies two values. |
/ | Division | Adds two values. |
% | Modulus | Permits remainder of a number division. |
++ | Increment | Increases the value of numbers by one. |
-- | Decrement | Decreases the value of numbers by one. |
The Addition Operator
The add operator can be used to directly add values or numbers. For example:
var z = 4 + 11; // Add 4 & 11, and assign the sum to z
Live Demo!
Now let's try adding variables:
var x = 3; // assign the value 3 to x
var y = 6; // assign the value 6 to y
var z = x + y; // assign the value 9 to z (3 + 6)
Live Demo!
Subtraction Operator
Subtracts the first variable/value from the second. Let's check an example:
//Subtracting values
var x = 13 - 5;
//Subtracting variables
var a = 12;
var b = 7;
var c = a - b;
Live Demo!
Multiplication Operator
The multiplication operator is used to multiply two numbers. Let's check an example:
var a = 3;
var b = 11;
var c = a * b;
// c now contains the product of 3 & 11
Live Demo!
Division Operator
This operator divides the integers. In the example below, x is divided by y, and the result is stored in z.
var x = 9;
var y = 3;
var z = x / y;
// z will have the result of division of x & y, which is 3
Live Demo!