Page Content

Tutorials

What are Arithmetic Operator in JavaScript Development

Arithmetic Operator in JavaScript

Arithmetic operators are a popular type of operator that are used to calculate mathematical values on operands. Usually, these operators deal with numbers. JavaScript may attempt automatic type conversion if operands are not integers. Operations may produce the NaN (Not a Number) value if non-numeric operands cannot be transformed into numbers.

Now let’s examine the particular arithmetic operators you enquired about:

  • Addition (+): The + operator adds when applied to numeric operands. For instance, 3 + 2 equals 5. When one or both operands are strings, + functions as a string concatenation operator, which is unique among the fundamental arithmetic operators. It connects the strings in this instance. Additionally, the + sign can be employed as a unary operator, which, if feasible, will convert its sole operand to a number.
  • Subtraction (-): Two numeric operands are subtracted using the – operator. Three minus two, for instance, equals one. Both + and – can be used as unary negation operators, transforming their operands to numbers and changing their signs.
  • Multiplication (*): * multiplies 2. For example, 3 * 2 = 6.
  • Division (/): Divides initial operand by second operand. For instance, 3 / 2 yields 1.5. In JavaScript, dividing by zero usually yields Infinity or -Infinity rather than an error.
  • Modulus (%): The % operator divides the first operand by the second to determine the remainder. For instance, 5% 2 equals 0.1. Additionally, floating-point values can be used with it. The first operand’s sign and the result’s sign are identical.

Operator precedence establishes the order of evaluation when combining arithmetic operators into a single expression.

Division (/), modulus (%), and multiplication (*) usually take precedence over addition (+) and subtraction. The default precedence can be overridden using brackets ((…)) to evaluate expressions first.

The following code example illustrates how to apply these arithmetic operators:

// Declare and assign initial values to variables 
let num1 = 20; // A number literal is an expression
let num2 = 4;  // Variables hold values
let result;    // Variables are named containers for data 
// --- Basic Arithmetic Operators ---
// Arithmetic operators perform mathematical operations on numbers
// Operands are the values the operators act on 
// Addition (+) 
result = num1 + num2; // Adds the values of num1 and num2 
console.log("Addition (20 + 4): " + result); // Output the result using console.log()
// Subtraction (-) 
result = num1 - num2; // Subtracts num2 from num1 
console.log("Subtraction (20 - 4): " + result);
// Multiplication (*) 
result = num1 * num2; // Multiplies num1 by num2 
console.log("Multiplication (20 * 4): " + result);
// Division (/)
result = num1 / num2; // Divides num1 by num2 
console.log("Division (20 / 4): " + result);
// Modulus (%) 
result = num1 % num2; // Returns the remainder of the division 
console.log("Modulus (20 % 4): " + result);
// Exponentiation (**) 
// Raises the first operand to the power of the second operand 
result = num1 ** 2; // 20 to the power of 2 (20 * 20)
console.log("Exponentiation (20 ** 2): " + result);
result = 8 ** (1/3); // Cube root of 8 (using fractional exponent) 
console.log("Exponentiation (8 ** (1/3) - cube root): " + result);

In conclusion, arithmetic operators are necessary for JavaScript computations. By adding, subtracting, multiplying, dividing, and determining the remainder, the operators +, -, *, /, and % convert expressions to numeric values. In string concatenation, the + operator is fundamental. Writing efficient JavaScript code requires an understanding of these operators’ functions as well as how precedence influences evaluation.