JavaScript: Arithmetic Operators

Arithmetic operator, is a type of operator used to do some simple mathematical stuff in the javascript programming language.

  • An arithmetic operator is comprised of many mathematical functions to do the math stuff.
  • Arithmetic Operators are ( +, -, *, /,  --, %, ++ ) ,  we will see one by one with a perfect code example to understand what is happening in a code while using arithmetic operators.

1. + (Addition)

The + is an operator that performs the additional math function using the operands/value we assign in a variable.

let num1 = 400;
let num2 = 500;

let sum = num1+num2;

console.log(sum)

Output:

900

2. - (Subtraction)

The - is an operator which performs the subtraction math function using some of the operands/values we assign in a variable same as the + function.

let num1 = 400;
let num2 = 500;

let diff = num2 - num1;

console.log(diff)

Output:

100

3. * (Multiplication)

The * is an operator which performs the multiplication math function using the operands we assign.

let num1 = 4;
let num2 = 5;

let mul = num1 * num 2;

console.log(mul)

Output:

20

4. / (Division)

The / operator divides the assigned operand/value.

let num1 = 20;
let num2 = 5;

let div = num1 / num2;

console.log(div)

Output:

4

5. % (Modulus)

The modulus operator % divides the provided value and gives out the remainder/leftover.

let num1 = 10;

let mod = num1 % 3;

console.log(mod)

Output:

2

6. ++ (Increment)

The ++ or increment operator increments/increase the provided value within a variable when initiated after the declaration.

let neoSalary = 20000;

neoSalary++

console.log(neoSalary)

Output:

20001

7. -- (Decrement)

The -- or decrement operator decrements/decrease the provided value within a variable when initiated after the declaration.

let neoSalary = 20000;

neoSalary--

console.log(neoSalary)

Output:

19999
Fri, 09/24/2021 - 11:56

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags