• JavaScript Basics

  • Operators in JavaScript

  • Conditional Statements in JavaScript

  • JavaScript Strings

  • JavaScript Arrays

  • JavaScript Loop

  • JavaScript Functions

  • Conclusion

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
1let a = 10;
2let b = 3;
3console.log(a + b); // Output: 13
4console.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
1let x = 10;
2x += 5;  // Equivalent to x = x + 5
3console.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
1let num1 = 5;
2let num2 = 10;
3console.log(num1 > num2); // Output: false
4console.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
1let firstName = "John";
2let lastName = "Doe";
3console.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
1let isAdult = true;
2let hasID = false;
3console.log(isAdult && hasID); // Output: false
4console.log(isAdult || hasID); // Output: true
5console.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
1let a = 5;  // 0101 in binary
2let b = 3;  // 0011 in binary
3console.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
1condition ? expressionIfTrue : expressionIfFalse;

Example:

javascript
1let age = 18;
2let message = (age >= 18) ? "You are an adult." : "You are a minor.";
3console.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
1let text = "Hello World";
2console.log(typeof text); // Output: string
3
4let date = new Date();
5console.log(date instanceof Date); // Output: true