JS Arithmetic Operators


Arithmetic Operators

·       Arithmetic operators are used to performing arithmetic calculations including addition (+), subtraction (-), multiplication (*), modulus (%), increment (++), decrement (--), etc.

1)    Addition (+)

·       Takes two or more numerical operands and gives their numerical values. It is also used to concatenate two or more strings or numbers.

var a =5;

let b= 10;

const c= "hello";

const d= "world!!";

 

// Number + Number => Addition

console.log(a+b); // 15

 

// Number + String => Concatenation

console.log(a+c); // 5hello

 

// String + String => Concatenation

console.log(c+d); // helloworld!!

 

2)   Subtraction (-) 

·       It gives the difference between two operands in the form of numerical value.

var a =25;

let b= 10;

console.log(a-b); // 15

 

3)   Multiplication (*) 

·       It gives the product of operands where one operand is multiplicated with another operand.

var a =25;

let b= 10;

console.log(a*b); // 250

 

4)   Division (/) 

·       Its operands where the right operand is divided into the left operand.

var a =25;

let b= 10;

console.log(a/b); // 2.5

 

5)   Modulus (%) 

·       It is the remainder of two operands.

·       The other name of the modulus operator is the remainder operator.

var a =25;

let b= 10;

console.log(a%b); // 5

 

6)   Exponentiation (**) 

·        It gives the result of raising the first operand to the power of the second operand.

// number ** number

var a =2;

let b= 10;

console.log(a**b); // 1024

 

7)   Increment (++)

Ø  It increases the values of the variable.

Postfix(++x): increases the value of x even before assigning it to the variable x.

var x =2;

//increment Postfix

console.log(++x); // 3

 

Prefix(x++): first returns the value of the variable x, after that the value of x is incremented by 1.

var x =2;

//increment Prefix

console.log(x++); // 2

 

8)    Decrement (--) 

·       It decreases the values of the variable.

Postfix (--x): decreases the value of x even before assigning it to the variable x.

var x =2;

//increment Prefix

console.log(--x); // 1

 

Prefix(x--): first returns the value of the variable x, after that the value of x is decremented by 1.

var x =2;

//increment Prefix

console.log(x--); // 2

 

9)   Unary 

·       A unary operation is an operation with only one operand. This operand comes either before or after the operator.

·       unary operators cannot be overridden.

Unary plus (+)

·       It is a simple plus sign (+). If you place the unary plus before a numeric value, it does nothing. 

let x = 100;

let y = +x;

// Unary plus

console.log(y); // 100

 

Unary Minus (-)

·       It is a single minus sign (-). If you apply the unary minus operator to a number, it negates the number.

let x = 100;

let y = -x;

//Unary Minus

console.log(y); // -100

 


Leave a comment
No Cmomments yet