Skip to main content

JavaScript Operators

JavaScript Operators

JavaScript operators are special symbols or keywords used to perform operations on operands (values or variables). They play a crucial role in manipulating data and controlling the flow of your program. This article covers the various types of operators available in JavaScript and their uses.


Types of JavaScript Operators

JavaScript operators can be categorized into several types based on their functionality:

1. Arithmetic Operators

Arithmetic operators perform mathematical operations on numbers. They include:

let a = 10;
let b = 5;

console.log(a + b); // Addition: 15
console.log(a - b); // Subtraction: 5
console.log(a * b); // Multiplication: 50
console.log(a / b); // Division: 2
console.log(a % b); // Modulus (remainder): 0
console.log(a ** b); // Exponentiation: 100000

Examples:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division
  • % Modulus
  • ** Exponentiation

2. Assignment Operators

Assignment operators are used to assign values to variables. They include:

let x = 10;
x += 5; // Addition assignment: x = x + 5 (15)
x -= 2; // Subtraction assignment: x = x - 2 (13)
x *= 3; // Multiplication assignment: x = x * 3 (39)
x /= 3; // Division assignment: x = x / 3 (13)
x %= 4; // Modulus assignment: x = x % 4 (1)
x **= 2; // Exponentiation assignment: x = x ** 2 (1)

Examples:

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

3. Comparison Operators

Comparison operators compare two values and return a boolean result. They include:

let a = 10;
let b = 5;

console.log(a == b); // Equality: false
console.log(a != b); // Inequality: true
console.log(a > b); // Greater than: true
console.log(a < b); // Less than: false
console.log(a >= b); // Greater than or equal to: true
console.log(a <= b); // Less than or equal to: false
console.log(a === 10); // Strict equality: true
console.log(a !== 10); // Strict inequality: false

Examples:

  • == Equality
  • != Inequality
  • > Greater than
  • < Less than
  • >= Greater than or equal to
  • <= Less than or equal to
  • === Strict equality
  • !== Strict inequality

4. Logical Operators

Logical operators perform logical operations and return a boolean result. They include:

let a = true;
let b = false;

console.log(a && b); // Logical AND: false
console.log(a || b); // Logical OR: true
console.log(!a); // Logical NOT: false

Examples:

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

5. Unary Operators

Unary operators operate on a single operand. They include:

let a = 10;

console.log(+a); // Unary plus: 10 (converts to number)
console.log(-a); // Unary minus: -10 (negates the value)
console.log(++a); // Increment: 11 (increases the value by 1)
console.log(--a); // Decrement: 10 (decreases the value by 1)

Examples:

  • + Unary plus
  • - Unary minus
  • ++ Increment
  • -- Decrement

6. Ternary Operator

The ternary operator is a shorthand for an if-else statement. It takes three operands and returns a value based on a condition:

let age = 18;
let canVote = (age >= 18) ? 'Yes' : 'No'; // Ternary operator: "Yes"

Examples:

  • condition ? expr1 : expr2 Ternary operator

Conclusion

JavaScript operators are fundamental tools for performing operations and manipulating values in your code. Understanding the different types of operators and their uses helps you write more efficient and effective JavaScript code. By mastering arithmetic, assignment, comparison, logical, unary, and ternary operators, you can handle various scenarios and build powerful applications.

Comments