Arithmetic Operators JavaScript

Arithmetic Operators in javascript

Arithmetic operators in JavaScript programming are used to perform mathematical calculations such as addition, subtraction, multiplication, division, subtraction, increment and decrement operators. You may already know arithmetic operators in many programming languages. You can use basic to advanced arithmetic operators in JavaScript to perform multiple numeric operations.

Arithmetic Operators JavaScript

So, let’s get to know the arithmetic operators in JavaScript programming language better.

Addition (+) Operator.

The plus or addition operator (+) is used to add operands in two different numeric values.

Example of plus operator in JavaScript.

let p = 1;

let q = 2;

let total = p + q;  // 1 + 2 = 3

console.log(total);  // result is – 3

JavaScript String Concatenation – In JavaScript programs, the + operator can be used to join two different string characters together. Where the + operator combines two different strings with another operand, whether it is a numeric or string text.

let str = “Vcanhelpsu”;

let value = 1;

let concat= str + value;  // “Vcanhelpsu” + 1 = “Vcanhelpsu1”

console.log(concat);  // result is – “Vcanhelpsu1”

Subtraction (-) Operator.

In JavaScript programming, the subtraction operator (-) subtracts the right operand from the left operand or a larger numeric value from a smaller numeric value.

Example of subtraction operator in JavaScript.

let p = 2;

let q = 1;

let substract = p – q;  // 2 – 1 = 1

console.log(substract);  // result is – 1

Multiplication (*) operator.

In JavaScript programming, the multiplication operator (*) is used to multiply different numeric values. Where the * operator multiplies two operands together.

Example of multiplication (*) operator in JavaScript.

let p = 4;

let q = 3;

let multiply = p * q;  // 4 * 3 = 12

console.log(multiply);  // result is – 12

Division (/) operator.

In JavaScript programming, the division operator (/) is used to divide two numeric values ​​together, where it divides the left operand by the right operand.

Example of Division (/) operator in JavaScript.

let p = 12;

let q = 3;

let division = p / q;  // 12 / 3 = 4

console.log(division);  // result is – 4

Number divided by zero – If the divisor value in a program is 0, JavaScript returns the value infinity or -infinity depending on the sign of the fraction, or returns NaN value when 0 is divided by 0.

let p = 4;

let q = 0;

console.log(p / q);  // result – Infinity

let m = 0;

let n = 0;

console.log(m / n);  // result – NaN Not-a-Number value

Modulus (%) Operator – Remainder of division.

In JavaScript programming, the modulus operator (%) in a program returns the reminder value when the left operand is divided by the right operand.

Example of modulus (%) operator in JavaScript.

let p = 11;

let q = 4;

let modulus = p % q;  // 11 % 4 = 3 remainder when 11 is divided by 4

console.log(modulus);  // result is – 3

Modulus operator in JavaScript is commonly used to check whether a numeric value is even or odd value, as it returns 0 output for even numeric and 1 value for odd number when a numeric value is divided by 2.

let value = 9;

if (value % 2 === 0) {

  console.log(“Even inetger”);

} else {

  console.log(“Odd integer”);

}

Exponent (**) operator.

The exponent operator (**) in JavaScript programs raises the left operand to the exponent of the right operand.

Example of exponentiation (**) operator in JavaScript.

let p = 3;

let q = 4;

let exponent = p ** q;  // 3 raised to the power of 4 = 81

console.log(exponent);  // result is – 81

Increment (++) operator.

The increment operator (++) in JavaScript program increases the active value of a declared program variable by 1.

Example of increment (++) operator in JavaScript.

let p = 9;

p++;  // p Increments a by 1

console.log(p);  // result is – 10

Use of increment operator in JavaScript.

  • Post -increment (p++) – It increments the p variable value in post increment order, but returns the value before the variable value increment.
  • Pre -increase (++p) – It increments the p variable value in pre-increment order, and returns the value after the variable value increment.

let p = 8;

let q = p++;  // Post-increment: q = 8, p becomes p

console.log(q);  // result is – 8

console.log(p);  // result is – 9

let m = 3;

let n = ++m;  // Pre-increment: m becomes 4, n = 4

console.log(n);  // result is – 4

console.log(m);  // result is – 4

Decrement (-) Operator.

The decrement operator (–) decreases the value of a variable by 1. It displays the current value of the existing variable by decrementing it by one value.

Example of Decrement Operator in JavaScript.

let p = 3;

p–;  // Decrements p by 1

console.log(p);  // result is – 2

Like the increment operator in JavaScript, the use of a decrement operator.

  • Post- Decrement (p–) – It decrements the variable value p, but it returns the value before the variable value decrement.
  • Pre-decrement (-p) – It decrements the variable value p, and returns the value after the variable value decrement.

let p = 7;

let q = p–;  // Post-decrement- q = 7, p becomes 6

console.log(q);  // result is – 7

console.log(p);  // result is – 6

let m = 8;

let n = –m;  // Pre-decrement- m becomes 7, n = 7

console.log(n);  // result is – 7

console.log(m);  // result is – 7

Operator preference in JavaScript.

In JavaScript programming, arithmetic operators follow a certain order of preference. Where as per the programmer’s need, in numeric operations, arithmetic operators like multiply (*), division (/), and modulus (%) are given preference over operators like add (+) and subtraction (-).

Operator preference JavaScript example.

Arithmetic Operators To modify the order of operations, you use brackets to group the elements of a multiply program operation expression.

let output = 3 + 7 * 3;  // Multiplication is performed first

console.log(output);  // result is – 24

let output = (3 + 4) * 7;  // Here Parentheses change the order of operations

console.log(output);  // result is – 49

Explanation of Arithmetic Operators.

OperatorDescriptionExampleResult
+Addition1 + 23
Subtraction7 – 16
*Multiplication4 * 312
/Division24 / 46
%Modulus (Remainder)10 % 31
**Exponentiation (Power)2 ** 38
++Increment (by 1)let p = 4; a++5
Decrement (by 1)let q = 3; q–2

Arithmetic Operator Conclusion in JavaScript.

Arithmetic operators in JavaScript programming allow programmers to perform fundamental arithmetic operations such as add, subtract, multiply, and division. JavaScript programmers can use these operators for advanced arithmetic calculations, such as modulus and exponent, as well as increment and decrement values.