JavaScript Operators


What are operators in JavaScript?

Operators in JavaScript are special symbols that perform operations on variables and values. They can be classified into several categories based on their functionality.


What are the different types of operators in JavaScript?

The main types of operators in JavaScript include:

  • Arithmetic Operators: Used for performing mathematical calculations.
  • Assignment Operators: Used to assign values to variables.
  • Comparison Operators: Used to compare two values.
  • Logical Operators: Used to perform logical operations.
  • Bitwise Operators: Operate on bits and perform bit-level operations.
  • Ternary Operator: A shorthand for the if-else statement.
  • Instanceof Operator: Tests whether an object is an instance of a specific class.

What are arithmetic operators? Provide examples.

Arithmetic operators are used to perform mathematical calculations. The common arithmetic operators are:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder)
  • **: Exponentiation (ES2016)

let a = 10;
let b = 5;
console.log(a + b); // 15
console.log(a - b); // 5
console.log(a * b); // 50
console.log(a / b); // 2
console.log(a % b); // 0
console.log(a ** 2); // 100

What are assignment operators? Provide examples.

Assignment operators are used to assign values to variables. Common assignment operators include:

  • =: Assigns the right operand to the left operand.
  • +=: Adds the right operand to the left operand and assigns the result.
  • -=: Subtracts the right operand from the left operand and assigns the result.
  • *=: Multiplies the left operand by the right operand and assigns the result.
  • /=: Divides the left operand by the right operand and assigns the result.
  • %=: Takes the modulus using two operands and assigns the result.

let x = 10;
x += 5; // x = x + 5
console.log(x); // 15
x *= 2; // x = x * 2
console.log(x); // 30

What are comparison operators? Provide examples.

Comparison operators are used to compare two values. The common comparison operators include:

  • ==: Equal to (with type coercion)
  • ===: Strictly equal to (without type coercion)
  • !=: Not equal to (with type coercion)
  • !==: Strictly not equal to (without type coercion)
  • >: Greater than
  • <: Less than
  • >=: Greater than or equal to
  • <=: Less than or equal to

console.log(5 == '5');    // true
console.log(5 === '5');   // false
console.log(5 != 10);      // true
console.log(5 !== 10);     // true
console.log(5 > 3);        // true

What are logical operators? Provide examples.

Logical operators are used to perform logical operations. The common logical operators include:

  • &&: Logical AND
  • ||: Logical OR
  • !: Logical NOT

let a = true;
let b = false;
console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false

What are bitwise operators? Provide examples.

Bitwise operators operate on binary representations of numbers. Common bitwise operators include:

  • &: Bitwise AND
  • |: Bitwise OR
  • ^: Bitwise XOR
  • ~: Bitwise NOT
  • <<: Left shift
  • >>: Right shift

let x = 5; // 0101 in binary
let y = 3; // 0011 in binary
console.log(x & y); // 1 (0001)
console.log(x | y); // 7 (0111)
console.log(x ^ y); // 6 (0110)
console.log(~x);     // -6 (inverts the bits)

What is the ternary operator in JavaScript?

The ternary operator is a shorthand for the if-else statement. It takes three operands: a condition, the result for true, and the result for false.


let age = 18;
let status = (age >= 18) ? 'Adult' : 'Minor';
console.log(status); // Adult

What is the instanceof operator in JavaScript?

The instanceof operator tests whether an object is an instance of a specific constructor or class. It returns true or false.


class Person {}
const john = new Person();
console.log(john instanceof Person); // true

How do you concatenate strings in JavaScript?

You can concatenate strings using the + operator or by using template literals.


let firstName = 'John';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName; // Using +
console.log(fullName); // John Doe

// Using template literals
let fullNameTemplate = `${firstName} ${lastName}`;
console.log(fullNameTemplate); // John Doe

What are compound assignment operators?

Compound assignment operators are shorthand operators that combine an arithmetic operation with assignment. They include:

  • +=: Addition assignment
  • -=: Subtraction assignment
  • *=: Multiplication assignment
  • /=: Division assignment
  • %=: Modulus assignment

let x = 10;
x += 5; // x = x + 5
console.log(x); // 15
Ads