JavaScript > JavaScript Fundamentals > Operators > Arithmetic operators
Basic Arithmetic Operations in JavaScript
This snippet demonstrates the fundamental arithmetic operators in JavaScript, showcasing addition, subtraction, multiplication, division, modulus, exponentiation, increment, and decrement operations with clear explanations and examples.
Introduction to Arithmetic Operators
Arithmetic operators are symbols that perform mathematical calculations on numerical operands. JavaScript provides a rich set of arithmetic operators to perform basic and advanced mathematical operations.
Addition (+)
The addition operator (+) adds two operands together. In this example, we add 'a' and 'b' to get their sum.
let a = 10;
let b = 5;
let sum = a + b;
console.log("Sum: ", sum); // Output: Sum: 15
Subtraction (-)
The subtraction operator (-) subtracts the second operand from the first. Here, we subtract 'b' from 'a'.
let a = 10;
let b = 5;
let difference = a - b;
console.log("Difference: ", difference); // Output: Difference: 5
Multiplication (*)
The multiplication operator (*) multiplies two operands together.
let a = 10;
let b = 5;
let product = a * b;
console.log("Product: ", product); // Output: Product: 50
Division (/)
The division operator (/) divides the first operand by the second. It returns a floating-point result even if both operands are integers.
let a = 10;
let b = 5;
let quotient = a / b;
console.log("Quotient: ", quotient); // Output: Quotient: 2
Modulus (%)
The modulus operator (%) returns the remainder of a division operation. It's useful for determining if a number is even or odd, or for wrapping values within a specific range.
let a = 10;
let b = 3;
let remainder = a % b;
console.log("Remainder: ", remainder); // Output: Remainder: 1
Exponentiation (**)
The exponentiation operator (**) raises the first operand to the power of the second operand. This is equivalent to Math.pow(a, b).
let a = 2;
let b = 3;
let power = a ** b;
console.log("Power: ", power); // Output: Power: 8
Increment (++)
The increment operator (++) increases the value of a variable by 1. It can be used as a prefix (++a) or postfix (a++) operator. The prefix version increments the value before returning it, while the postfix version increments after returning the original value.
let a = 5;
a++;
console.log("Incremented value: ", a); // Output: Incremented value: 6
Decrement (--)
The decrement operator (--) decreases the value of a variable by 1. Similar to the increment operator, it has prefix (--a) and postfix (a--) versions with the same difference in behavior.
let a = 5;
a--;
console.log("Decremented value: ", a); // Output: Decremented value: 4
Concepts Behind the Snippet
This snippet covers fundamental arithmetic operators: addition, subtraction, multiplication, division, modulus, exponentiation, increment, and decrement. Understanding these operators is crucial for performing mathematical calculations and manipulating numerical data in JavaScript.
Real-Life Use Case Section
Arithmetic operators are used extensively in various scenarios, such as calculating prices in e-commerce applications, performing data analysis, implementing game logic, and manipulating graphics. For example, calculating the total cost of items in a shopping cart involves addition and multiplication.
Best Practices
Interview Tip
Be prepared to explain the difference between prefix and postfix increment/decrement operators. Understand the order of operations (PEMDAS/BODMAS) and how parentheses affect the evaluation of expressions.
When to Use Them
Use arithmetic operators whenever you need to perform mathematical calculations or manipulate numerical data. Choose the appropriate operator based on the desired operation (addition, subtraction, multiplication, division, etc.).
Memory Footprint
Arithmetic operators themselves have a minimal memory footprint. However, the memory usage depends on the size and number of numerical variables being used in the calculations. Using large numbers or creating many variables can increase memory consumption.
Alternatives
While arithmetic operators are the primary way to perform calculations, you can also use built-in Math functions for more complex operations (e.g., Math.pow, Math.sqrt, Math.sin, Math.cos). Libraries like Math.js provide extended mathematical functionality.
Pros
Cons
FAQ
-
What is the order of operations in JavaScript?
JavaScript follows the standard order of operations, often remembered by the acronym PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction). Expressions in parentheses are evaluated first, followed by exponents, then multiplication and division (from left to right), and finally addition and subtraction (from left to right). -
What happens if I divide by zero?
Dividing a non-zero number by zero in JavaScript results in 'Infinity' (positive or negative, depending on the sign of the dividend). Dividing zero by zero results in 'NaN' (Not a Number). -
How do I handle floating-point precision issues?
Floating-point numbers in JavaScript (and many other languages) can have precision issues due to the way they are stored in memory. To mitigate this, you can use methods like 'toFixed()' to round the result to a specific number of decimal places, or use libraries designed for high-precision arithmetic.