Operators

Operators in JavaScript are used to perform operations on our variables and values. They are essential for performing arithmetic calculations, comparing values, assigning values, and more. Understanding how to use operators is crucial for writing effective JavaScript code.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more.

  • 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 operation.

  • Increment (++): Increases a number by one.

  • Decrement (-): Decreases a number by one (same as subtraction, yes).

Examples:

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

let x = 1;
x++;
console.log(x); // 2

let y = 2;
y--;
console.log(y); // 1

2. Comparison Operators

Comparison operators are used to compare two values and return a boolean value (true or false). They are commonly used in conditional statements to perform different actions based on different conditions.

  • Equal to (==): Compares two values for equality, ignoring data type.

  • Strict equal to (===): Compares two values for equality, considering data type.

  • Not equal to (!=): Compares two values for inequality, ignoring data type.

  • Strict not equal to (!==): Compares two values for inequality, considering data type.

  • Greater than (>): Checks if the left value is greater than the right value.

  • Less than (<): Checks if the left value is less than the right value.

  • Greater than or equal to (>=): Checks if the left value is greater than or equal to the right value.

  • Less than or equal to (<=): Checks if the left value is less than or equal to the right value.

Examples:

let a = 10;
let b = "10";

console.log(a == b);  // true (ignores data type)
console.log(a === b); // false (considers data type)
console.log(a != b);  // false (ignores data type)
console.log(a !== b); // true (considers data type)
console.log(a > 5);   // true
console.log(a < 5);   // false
console.log(a >= 10); // true
console.log(a <= 10); // true

3. Logical Operators

Logical operators are used to combine multiple conditions. They are often used in control flow statements to create more complex conditions (more info).

  • Logical AND (&&): Returns true if both conditions are true.

  • Logical OR (||): Returns true if at least one condition is true.

  • Logical NOT (!): Returns the opposite boolean value of the condition.

Examples:

let isAdult = true;
let hasTicket = false;

console.log(isAdult && hasTicket); // false (both conditions must be true)
console.log(isAdult || hasTicket); // true (at least one condition is true)
console.log(!isAdult); // false (opposite of true)

4. Assignment Operators

Assignment operators are used to assign values to variables. They include the basic assignment operator as well as compound assignment operators that combine arithmetic operations with assignment.

  • Assignment (=): Assigns a value to a variable.

  • Addition assignment (+=): Adds a value to a variable and assigns the result to the variable.

  • Subtraction assignment (=): Subtracts a value from a variable and assigns the result to the variable.

  • Multiplication assignment (=): Multiplies a variable by a value and assigns the result to the variable.

  • Division assignment (/=): Divides a variable by a value and assigns the result to the variable.

  • Modulus assignment (%=): Takes the modulus of a variable by a value and assigns the result to the variable.

Examples:

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

let b = 20;
b -= 5; // b = b - 5
console.log(b); // 15

let c = 3;
c *= 2; // c = c * 2
console.log(c); // 6

let d = 10;
d /= 2; // d = d / 2
console.log(d); // 5

let e = 10;
e %= 3; // e = e % 3
console.log(e); // 1

5. Ternary Operator

The ternary operator is a shorthand for the if statement (next chapter). It allows you to perform a simple conditional assignment or operation.

  • Ternary operator (condition ? expr1 : expr2): Evaluates a condition and returns expr1 if the condition is true, and expr2 if the condition is false.

Example:

let age = 20;
let canVote = age >= 18 ? "Yes" : "No";
console.log(canVote); // "Yes"

Use Case:

Suppose you're building a simple application to check if a user is eligible to vote based on their age. You might use various operators to perform this check and display the result.

Example:

let userAge = 20;

let isEligibleToVote = userAge >= 18 ? "Yes" : "No";
console.log("Eligible to vote: " + isEligibleToVote); // "Eligible to vote: Yes"

In this example:

  • We declare a variable userAge to store the age of the user.

  • We use the ternary operator to check if the user is 18 or older and assign the result to the isEligibleToVote variable.

  • We use console.log to print whether the user is eligible to vote.

This use case demonstrates how different operators can be used to perform checks and make decisions in your code. Understanding these operators is essential for building logical and functional JavaScript programs.


Help us improve the content 🤩

You can leave comments here.

Last updated