Arithmetic operators in JavaScript are used to perform mathematical calculations on numeric operands. JavaScript supports standard arithmetic operators for addition, subtraction, multiplication, division, and more. Let's explore these arithmetic operators:
The addition operator ( + ) adds two numeric values or concatenates two strings.
let sum = 5 + 3; // Result: 8 (numeric addition)
let fullName = "John" + " " + "Doe"; // Result: "John Doe" (string concatenation)
The subtraction operator ( - ) subtracts the second operand from the first.
let difference = 10 - 5; // Result: 5
The multiplication operator ( * ) multiplies two numeric values.
let product = 3 * 4; // Result: 12
The division operator (`/`) divides the first operand by the second.
let quotient = 12 / 3; // Result: 4
The modulus operator (`%`) returns the remainder of the division operation.
let remainder = 10 % 3; // Result: 1
The exponentiation operator ( ** ) raises the first operand to the power of the second operand.
let result = 2 ** 3; // Result: 8 (2 raised to the power of 3)
Example Usage:
Here's an example demonstrating the use of arithmetic operators in JavaScript:
let a = 10;
let b = 5;
let sum = a + b; // Result: 15
let difference = a - b; // Result: 5
let product = a * b; // Result: 50
let quotient = a / b; // Result: 2
let remainder = a % b; // Result: 0
console.log("Sum:", sum);
console.log("Difference:", difference);
console.log("Product:", product);
console.log("Quotient:", quotient);
console.log("Remainder:", remainder);
Arithmetic operators are fundamental in JavaScript for performing mathematical calculations and manipulating numeric values. Understanding how to use these operators allows you to perform various arithmetic tasks in your JavaScript programs. Whether you're adding numbers, concatenating strings, or finding remainders, arithmetic operators are essential tools for working with numerical data.