JavaScript Arithmetic Operators
Subject: JavaScript
Arithmetic operators in JavaScript are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more. These are commonly used when working with numeric values.
List of Arithmetic Operators in JavaScript
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3.333... |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation (Power) | 10 ** 3 | 1000 |
++ | Increment | a++ | a + 1 |
-- | Decrement | b-- | b - 1 |
Example: Arithmetic in JavaScript
Explanation of Operators
- Addition (
+): Combines two values. Works with numbers and strings. - Subtraction (
-): Subtracts the second value from the first. - Multiplication (
*): Multiplies the values. - Division (
/): Divides the first number by the second. - Modulus (
%): Returns the remainder of a division. - Exponentiation (
**): Raises a number to the power of another. - Increment (
++): Increases a variable's value by 1. - Decrement (
--): Decreases a variable's value by 1.
Key Takeaways
- Arithmetic operators allow for performing mathematical operations in JavaScript.
- Use
++and--for increment/decrement operations. %helps when checking divisibility or remainder.**provides an easy way to compute powers (e.g., squares, cubes).- These operators work with both integers and floating-point numbers.