JavaScript Operators

JavaScript operators are symbols or keywords that perform operations on values or variables. They allow you to manipulate data and variables in various ways. In this lesson, we’ll cover the main types of JavaScript operators, explaining how they work and providing examples for each.

Types of JavaScript Operators

1. Arithmetic Operators

Arithmetic operators perform basic mathematical operations such as addition, subtraction, multiplication, and division.

Common Arithmetic Operators:

  • + (Addition): Adds two numbers.
  • - (Subtraction): Subtracts one number from another.
  • * (Multiplication): Multiplies two numbers.
  • / (Division): Divides one number by another.
  • % (Modulus): Returns the remainder of a division.
  • ++ (Increment): Increases a number by one.
  • -- (Decrement): Decreases a number by one.

Example:

javascript
1
2
3
4
let a = 10;
let b = 3;
console.log(a + b); // Output: 13
console.log(a % b); // Output: 1

2. Assignment Operators

Assignment operators assign values to variables. The most common assignment operator is =, but there are others for different operations.

Common Assignment Operators:

  • = (Assignment): Assigns a value to a variable.
  • += (Addition Assignment): Adds and assigns a value.
  • -= (Subtraction Assignment): Subtracts and assigns a value.
  • *= (Multiplication Assignment): Multiplies and assigns a value.
  • /= (Division Assignment): Divides and assigns a value.
  • %= (Modulus Assignment): Assigns the remainder of a division.

Example:

javascript
1
2
3
let x = 10;
x += 5;  // Equivalent to x = x + 5
console.log(x); // Output: 15

3. Comparison Operators

Comparison operators compare two values and return a Boolean (true or false).

Common Comparison Operators:

  • == (Equal to): Checks if values are equal.
  • === (Strict Equal to): Checks if values and types are equal.
  • != (Not equal): Checks if values are not equal.
  • !== (Strict Not equal): Checks if values and types are not equal.
  • > (Greater than): Checks if the left value is greater.
  • < (Less than): Checks if the left value is smaller.
  • >= (Greater than or equal to): Checks if the left value is greater or equal.
  • <= (Less than or equal to): Checks if the left value is smaller or equal.

Example:

javascript
1
2
3
4
let num1 = 5;
let num2 = 10;
console.log(num1 > num2); // Output: false
console.log(num1 <= num2); // Output: true

4. String Operators

String operators are used to concatenate (join) strings. The + operator is commonly used for this purpose.

Common String Operators:

  • + (Concatenation): Joins two or more strings.
  • += (Concatenation Assignment): Adds a string to an existing string.

Example:

javascript
1
2
3
let firstName = "John";
let lastName = "Doe";
console.log(firstName + " " + lastName); // Output: John Doe

5. Logical Operators

Logical operators are used to perform logical operations and return Boolean values.

Common Logical Operators:

  • && (Logical AND): Returns true if both conditions are true.
  • || (Logical OR): Returns true if at least one condition is true.
  • ! (Logical NOT): Reverses the truth value.

Example:

javascript
1
2
3
4
5
let isAdult = true;
let hasID = false;
console.log(isAdult && hasID); // Output: false
console.log(isAdult || hasID); // Output: true
console.log(!isAdult); // Output: false

6. Bitwise Operators

Bitwise operators perform operations on binary numbers.

Common Bitwise Operators:

  • & (AND): Performs a binary AND.
  • | (OR): Performs a binary OR.
  • ^ (XOR): Performs a binary XOR.
  • ~ (NOT): Inverts all the bits.
  • << (Left Shift): Shifts bits to the left.
  • >> (Right Shift): Shifts bits to the right.

Example:

javascript
1
2
3
let a = 5;  // 0101 in binary
let b = 3;  // 0011 in binary
console.log(a & b); // Output: 1 (0001 in binary)

7. Ternary Operator

The ternary operator is a shorthand for the if...else statement and is represented by ? :.

Syntax:

javascript
1
condition ? expressionIfTrue : expressionIfFalse;

Example:

javascript
1
2
3
let age = 18;
let message = (age >= 18) ? "You are an adult." : "You are a minor.";
console.log(message); // Output: You are an adult.

8. Type Operators

Type operators help in checking or converting types of variables.

Common Type Operators:

  • typeof: Returns the type of a variable.
  • instanceof: Checks if an object is an instance of a specific class or constructor.

Example:

javascript
1
2
3
4
5
let text = "Hello World";
console.log(typeof text); // Output: string

let date = new Date();
console.log(date instanceof Date); // Output: true

Frequently Asked Questions