Understanding JavaScript Operations: A Complete Guide

Understanding JavaScript Operations: A Complete Guide

Operations

In JavaScript, various operations allow us to perform calculations and manipulate data effectively. These operations are executed using operators, which act on operands to carry out the desired tasks.

What Are Operators and Operands?

  • Operands: The values or variables on which an operation is performed.

  • Operators: Symbols or keywords that denote the operation to be performed, such as addition or multiplication.

Example:

If we have two variables, a and b:

  • a and b are operands.

  • + is the operator, performing the addition operation.

Operands : Operands mean the entities on which operations are being performed.

Plus is our operator : Operator means the one that will perform the operation (in this case, the operation being performed is addition).

Operation means any work that is being performed.

Types of Operations

1. Addition (+)

The addition operator sums two operands.

Example:

let a = 10;
let b = 5;
let sum = a + b;
console.log(sum); // Output: 15

2. Subtraction (-)

The subtraction operator calculates the difference between two operands.

Example:

let a = 10;
let b = 5;
let difference = a - b;
console.log(difference); // Output: 5

3. Multiplication (*)

The multiplication operator multiplies two operands.

Example:

let a = 10;
let b = 5;
let product = a * b;
console.log(product); // Output: 50

4. Division (/)

The division operator divides the first operand by the second.

Example:

let a = 10;
let b = 5;
let quotient = a / b;
console.log(quotient); // Output: 2

5. Modulo (%)

The modulo operator calculates the remainder of the division between two numbers.

Example:

let a = 10;
let b = 3;
let remainder = a % b;
console.log(remainder); // Output: 1

Use of Modulo for Odd and Even Numbers

  • Odd Numbers: A number is odd if number % 2 is not equal to 0.

  • Even Numbers: A number is even if number % 2 equals 0.

Example:

let number = 7;
if (number % 2 === 0) {
  console.log("Even");
} else {
  console.log("Odd");
}

6. Exponentiation (**)

The exponentiation operator raises the first operand to the power of the second.

Syntax:

let result = base ** exponent;

Example:

let base = 2;
let exponent = 3;
console.log(base ** exponent); // Output: 8
  • Modulo is most commonly used in JavaScript to detect odd and even numbers.

  • With the help of the modulo operator, we can determine which number is {odd} and which is {even}.

Odd and Even

Odd: Whenever we perform a modulo operation of any number (n) with 2,

The number will not be completely divisible by 2 and there will always be some remainder left.

If our odd number is not completely divisible by 2 and the remainder left is {something different from zero},

This means our number becomes odd.

  • And any number that is not completely divisible by 2 {with a remainder different from zero} is considered an odd number.

Even: Whenever we perform a modulo operation of any number (n) with 2 and the result is zero,

Example: We take any number, divide by 2, and if the remainder is zero, it means it is completely divisible.

  • And any number that is completely divisible by 2 is considered an even number.

    Exponentiation (Power Operator)

    The exponentiation power operator is used when we need to calculate one number raised to the power of another number.

    Syntax

    2**3 = 8 Explain - This means (2 \times 2 \times 2 = 8)

    This expression explains that when 2 is raised to the power of 3, it is equivalent to multiplying 2 by itself three times, which results in 8.