Arithmetic operators in JavaScript

Arithmetic operators are fundamental elements in JavaScript programming, facilitating mathematical calculations within scripts. They enable you to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and more. In this chapter, we'll explore these operators comprehensively, from the basics to advanced usage, with ample examples to solidify your understanding.

What are Arithmetic Operators?

JavaScript offers a rich set of operators specifically designed for performing mathematical computations. These operators manipulate numerical values to produce a new result.

Why are they Important?

Arithmetic operators are fundamental building blocks for various programming tasks, including calculations, data manipulation, and creating dynamic web content.

Basic Arithmetic Operators

Addition (+)

  • Adds two numbers.
				
					let sum = 10 + 5;
console.log(sum); // Output: 15

				
			

Subtraction (-)

  • Subtracts the second number from the first.
				
					let difference = 20 - 7;
console.log(difference); // Output: 13

				
			

Multiplication (*)

  • Multiplies two numbers.
				
					let product = 3 * 4;
console.log(product); // Output: 12

				
			

Division (/)

  • Divides the first number (dividend) by the second number (divisor). In JavaScript, division always results in a floating-point number, even if both operands are integers.
				
					let quotient = 16 / 4;
console.log(quotient); // Output: 4

				
			

Modulus (%)

  • Returns the remainder after dividing the first number (dividend) by the second number (divisor). This operator is useful for finding even/odd numbers or extracting specific digits.
				
					let remainder = 11 % 3;
console.log(remainder); // Output: 2 (11 divided by 3 leaves a remainder of 2)

				
			

Advanced Arithmetic Operators

Increment (++)

  • Increments (increases by 1) the value of a variable. It can be used in two ways:
  • Pre-increment (++x): Increments the value and then returns the new value.
  • Post-increment (x++): Returns the current value of x and then increments it.
				
					let count = 5;
console.log(count++); // Output: 5 (post-increment returns the current value)
console.log(count);   // Output: 6 (count is now incremented to 6)

let num = 1;
console.log(++num);  // Output: 2 (pre-increment increments first and returns the new value)

				
			

Decrement (–)

  • Decrements (decreases by 1) the value of a variable. Similar to increment, it has pre-decrement (--x) and post-decrement (x--) forms.
				
					let points = 10;
console.log(points--); // Output: 10 (post-decrement returns the current value)
console.log(points);   // Output: 9 (points is now decremented to 9)

let lives = 3;
console.log(--lives);  // Output: 2 (pre-decrement decrements first and returns the new value)

				
			

Exponentiation ()**

  • Raises the first number (base) to the power of the second number (exponent).
				
					let base = 2;
let exponent = 3;
let result = base ** exponent;
console.log(result); // Output: 8 (2 raised to the power of 3)

				
			

Operator Precedence

  • In expressions involving multiple operators, JavaScript follows a specific order of operations (precedence) to determine the calculation order. Operators with higher precedence are evaluated first.
  • The common precedence order (from highest to lowest) is:
    1. Exponentiation (**)
    2. Multiplication (*) and Division (/)
    3. Addition (+) and Subtraction (-)
  • Use parentheses () to override the default precedence and force calculations to be done in a specific order.
				
					let expr1 = 10 + 20 * 3;  // Evaluates to 70 (multiplication first)
let expr2 = (10 + 20) * 3; // Evaluates to 90 (

				
			

In essence, arithmetic operators empower you to perform essential mathematical computations within your JavaScript programs. By mastering these operators, you can manipulate numerical data, create dynamic calculations, and solve problems effectively. Remember operator precedence to ensure your expressions are evaluated correctly, and leverage parentheses for finer control over calculation order. With this knowledge, you're well-equipped to harness the power of arithmetic operations in JavaScript! Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India