Menu Close

JavaScript Arithmetic Operators

Posted in Javascript Tutorial

Arithmetic operators in JavaScript are used to perform various mathematical operations on numeric values. Here are the common arithmetic operators and examples of their usage:

  1. Addition (+): Adds two or more numbers.
    let sum = 5 + 3; // sum is 8
  2. Subtraction (-): Subtracts the right operand from the left operand.
    let difference = 10 - 4; // difference is 6
    
  3. Multiplication (*): Multiplies two or more numbers.
    let product = 6 * 7; // product is 42
  4. Division (/): Divides the left operand by the right operand.
    let quotient = 24 / 4; // quotient is 6
  5. Modulus (Remainder) (%): Returns the remainder of the division of the left operand by the right operand.
    let remainder = 17 % 5; // remainder is 2
    
  6. Exponentiation ()**: Raises the left operand to the power of the right operand. (Introduced in ECMAScript 6)
    let result = 2 ** 3; // result is 8 (2 raised to the power of 3)
    
  7. Increment (++): Increases the value of a variable by 1. It can be used as a postfix or prefix operator.
    let x = 5;
    x++; // x is now 6
    
  8. Decrement (–): Decreases the value of a variable by 1. It can be used as a postfix or prefix operator.
    let y = 10;
    y--; // y is now 9
    

Arithmetic operators are fundamental for performing mathematical calculations in JavaScript, whether it’s for simple arithmetic or more complex computations. You can use these operators in various combinations to solve mathematical problems and manipulate numeric data in your code.

JavaScript Arithmetic Operator  Example

// Addition Operator 
let a = 1 + 2 
console.log(a); 
  
// Subtraction Operator 
let b = 10 - 7 
console.log(b); 
  
// Multiplication Operator 
let c = 3 * 3 
console.log(c); 
  
// Division Operator 
let d = 1.0 / 2.0 
console.log(d); 
  
// Modulas Operator 
let e = 9 % 5 
console.log(e) 
  
// Exponentian Operator 
let f = 2 ** 5 
console.log(f) 
  
// Increment Operator 
let g = 2; 
g1 = g++; 
console.log(g) 
  
// Decrement Operator 
let h = 2; 
h1 = h--; 
console.log(h) 
  
// Unary plus Operator 
let i = 3; 
i1 = +i; 
console.log(i1) 
  
// Negation Operator 
let j = 3; 
j1 = -j; 
console.log(j1)

Results:

3
3
9
0.5
4
32
3
1
3
-3

Leave a Reply