Geek Slack

Introduction to JavaScript
    About Lesson



    JavaScript Operators


    JavaScript Operators

    JavaScript operators are symbols that perform operations on variables and values. JavaScript supports several types of operators, including arithmetic, assignment, comparison, logical, and more.

    1. Arithmetic Operators

    Arithmetic operators are used to perform mathematical operations.

    Example:

    let a = 10;
    let b = 5;
    
    let sum = a + b; // Addition
    let difference = a - b; // Subtraction
    let product = a * b; // Multiplication
    let quotient = a / b; // Division
    let remainder = a % b; // Modulus
    
    console.log(sum); // Output: 15
    console.log(difference); // Output: 5
    console.log(product); // Output: 50
    console.log(quotient); // Output: 2
    console.log(remainder); // Output: 0

    2. Assignment Operators

    Assignment operators assign values to variables.

    Example:

    let x = 10;
    x += 5; // x = x + 5
    x -= 3; // x = x - 3
    x *= 2; // x = x * 2
    x /= 4; // x = x / 4
    x %= 2; // x = x % 2
    
    console.log(x); // Output: 1

    3. Comparison Operators

    Comparison operators are used to compare two values and return a boolean result.

    Example:

    let a = 10;
    let b = 5;
    
    console.log(a == b); // Equal to (false)
    console.log(a != b); // Not equal to (true)
    console.log(a === b); // Strict equal to (false)
    console.log(a !== b); // Strict not equal to (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)

    4. Logical Operators

    Logical operators are used to combine multiple boolean expressions.

    Example:

    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)

    5. Bitwise Operators

    Bitwise operators perform operations on binary representations of numbers.

    Example:

    let a = 5; // 0101 in binary
    let b = 1; // 0001 in binary
    
    console.log(a & b); // Bitwise AND (0001, Output: 1)
    console.log(a | b); // Bitwise OR (0101, Output: 5)
    console.log(a ^ b); // Bitwise XOR (0100, Output: 4)
    console.log(~a); // Bitwise NOT (1010, Output: -6)
    console.log(a << 1); // Left shift (1010, Output: 10)
    console.log(a >> 1); // Right shift (0010, Output: 2)

    6. String Operators

    The + operator can also be used to concatenate (combine) strings.

    Example:

    let firstName = "John";
    let lastName = "Doe";
    
    let fullName = firstName + " " + lastName;
    
    console.log(fullName); // Output: John Doe

    7. Conditional (Ternary) Operator

    The conditional operator assigns a value to a variable based on a condition.

    Example:

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

    8. Type Operators

    Type operators are used to identify the type of a variable or object.

    Example:

    let name = "John";
    let age = 30;
    let isStudent = true;
    
    console.log(typeof name); // Output: string
    console.log(typeof age); // Output: number
    console.log(typeof isStudent); // Output: boolean

    9. Logical Nullish Assignment

    The logical nullish assignment operator (??=) only assigns a value to a variable if it is null or undefined.

    Example:

    let x = null;
    x ??= 10;
    
    let y = 5;
    y ??= 10;
    
    console.log(x); // Output: 10
    console.log(y); // Output: 5

    By understanding and using these operators, you can write more efficient and effective JavaScript code.