Operators in JavaScript: Arithmetic and Assignment Operators

Operators in JavaScript: Arithmetic and Assignment Operators

·

4 min read

Operators in JavaScript

Operators are special symbols in JavaScript that are used to perform operations on values. There are several types of operators in JavaScript.

1. Arithmetic Operators:

Arithmetic operators are used to perform mathematical operations on numbers, such as addition, subtraction, multiplication, division, exponentiation, modulus (remainder), increment, and decrement operators.

Addition Operator (+):

It is used to add two numbers.

// Addtion Operator Example
let x = 20;
let y = 10;
console.log(x + y); // It prints 30 in the output.

Subtraction Operator (-):

It is used to subtract two numbers.

// Subtraction Operator Example
let x = 20;
let y = 10;
console.log(x - y); // It prints 10 in the output.

Multiplication Operator (*):

It is used to multiply two numbers.

// Multiplication Operator Example
let x = 20;
let y = 10;
console.log(x * y); // It prints 100 in the output.

Division Operator (/):

It is used to divide two numbers.

// Division Operator Example
let x = 10;
let y = 10;
console.log(x / y); // It prints 2 in the output.

Modulus Operator (%):

It is used to return the remainder after division between two numbers.

let x = 9;
let y = 2;
console.log(x / y); // It prints 1 in the output as a remainder.

Exponentiation Operator (**):

It returns the power of any number.

// Exponentiation Operator Example
let x = 9;
let y = x**2;  // its mean x raise to the power 2
console.log(y); // It prints 81 in the output as result.

Increment Operator (++):

This operator adds 1 to the operand or increases the operand’s value by 1.

// Increment Operator Example
let x = 9;
x++; 
console.log(x); // It prints 10 in the output as result.

Decrement Operator (--):

This operator subtracts 1 from the operand or decreases the operand’s value by

// Decrement Operator Example
let x = 9;
x--; 
console.log(x); // It prints 8 in the output as result.

2. Assignment Operators:

The simple assignment operator (=):

A simple assignment operator is used to assign a value to a variable.

 // Simple Assignment Operator Example
    let x = 9; 
    console.log(x); // It prints 9 in the output as result.

Addition assignment operator (+=):

The addition assignment operator adds a value to a variable.

 // Addition Assignment Operator Example
    let x = 9;
    x += 5; // its mean x = x + 5; 
    console.log(x); // It prints 14 in the output as result.

Subtraction assignment operator (-=):

The subtraction assignment operator subtracts a value from a variable.

 // Subtraction Assignment Operator Example
    let x = 9;
    x -= 5; // its mean x = x - 5; 
    console.log(x); // It prints 4 in the output as result.

Multiplication assignment operator (*=):

The multiplication assignment operator multiplies a value by a variable.

 // Multiplication Assignment Operator Example
    let x = 9;
    x *= 5; // its mean x = x * 5; 
    console.log(x); // It prints 45 in the output as result.

Division assignment operator (/=):

The division assignment operator divides a value into a variable.

 // Division Assignment Operator Example
    let x = 9;
    x /= 3; // its mean x = x / 3; 
    console.log(x); // It prints 3 in the output as result.

Exponentiation assignment operator (*=):

The exponentiation assignment operator returns the power of a value to a variable.

 // Exponentiation Assignment Operator Example
    let x = 9;
    x **= 3; // its mean x = x ** 3; or x raise to the power 3
    console.log(x); // It prints 729 in the output as result.

Modulus assignment operator (*=):

The modulus assignment operator returns the remainder of a value to a variable.

 // Modulus Assignment Operator Example
    let x = 10;
    x %= 3; // its mean x = x % 3; 
    console.log(x); // It prints 1 in the output as a remainder.
Â