Arithmetic operations in JavaScript examples; In this tutorial, you will learn what are JavaScript Arithmetic Operators and how to use arithmetic operators in javascript.
Arithmetic operators in javascript
Arithmetic operators are used to performing mathematical operations between numeric operands.
Types of JavaScript Arithmetic Operators
JavaScript provides some arithmetic operators such as
- Addition (+)
- Subtraction (-)
- Division (/)
- Remainder (%)
- Multiplication (*)
- Increment (++)
- Decrement (
--
)
List of arithmetic operators in javascript
Operator | Description |
---|---|
+ | Adds two numeric operands. |
– | Subtract right operand from left operand |
* | Multiply two numeric operands. |
/ | Divide left operand by right operand. |
% | Modulus operator. Returns remainder of two operands. |
++ | Increment operator. Increase operand value by one. |
— | Decrement operator. Decrease value by one. |
Addition between operands
If the operands are numbers, JavaScript +
operator performs addition between two or more operands.
See the following example:
var a = 10; var b = 20; console.log(a + b); // 30
The +
operator performs concatenation operation when one of the operands is contained a string value.
See the following example:
var a = 50, b = "Hello ", c = 15; a + b; // "5Hello " a + c; // 65
Subtraction between operands
JavaScript -
operator performs a subtraction between operands.
See the following example:
var a = 10; var b = 20; console.log(b - a); // 10
Division between operands
JavaScript /
operator performs a division between operands.
See the following example:
var a = 10; var b = 20; console.log(b / a); // 2
If you divide by zero, JavaScript does not raise any error but returns the Infinity
value.
See the following example:
var a = 0; var b = 20; console.log(b / a); // Infinity
Remainder between operands
How to perform javascript%
operator between operands.
See the following example:
var a = 5; var b = 20; console.log(b % a); //zero
A reminder by zero is always NaN
, a special value that means “Not a Number”.
See the following example:
var a = 0; var b = 20; console.log(b % a); //NaN
Multiplication between operands
Javascript *
operator performs multiplication between operands.
See the following example:
var a = 5; var b = 20; console.log(b * a); //100
If you multiply by zero is always 0
.
Consider the following example:
var a = 0; var b = 20; console.log(b * a); //0
Increment with operand
This is a unary operator, and if put before the number, it returns the value incremented.
See the following example:
var a = 0; var b = 0; console.log(a++); //0 console.log(++b); //1
Decrement with operand
This is a unary operator, and if put before the number, it returns the value decremented.
See the following example:
var a = 2; var b = 2; console.log(a--); //2 console.log(--b); //1
Conclusion
In this tutorial, you have learned what is javascript arithmetic operators and how to use JavaScript arithmetic operators.