JavaScript+Operators

JavaScript Operators

Arithmetic Operators

  • Description: Used to perform mathematical calculations.

  • Operators and Examples:

    • + (Addition):

      • Example: X = 2, Y = 3; X + Y

      • Result: 5

    • - (Subtraction):

      • Example: X = 6, Y = 3; X - Y

      • Result: 3

    • * (Multiplication):

      • Example: X = 2, Y = 3; X * Y

      • Result: 6

    • / (Division):

      • Example: X = 9, Y = 3; X / Y

      • Result: 3 (For X = 5, Y = 2; X / Y Result: 2.5)

    • % (Modulus) (Remainder):

      • Example: 5 % 2 (Result: 1), 10 % 8 (Result: 2), 10 % 2 (Result: 0)

    • ++ (Increment):

      • Example: X = 5; X++

      • Result: 6

    • -- (Decrement):

      • Example: X = 5; X--

      • Result: 4

Assignment Operators

  • Description: Used to assign values to variables.

  • Operators and Examples:

    • = (Simple Assignment):

      • Example: x = y

    • += (Addition Assignment):

      • Example: x += y

      • Equivalent to: x = x + y

    • -= (Subtraction Assignment):

      • Example: x -= y

      • Equivalent to: x = x - y

    • *= (Multiplication Assignment):

      • Example: x *= y

      • Equivalent to: x = x * y

    • /= (Division Assignment):

      • Example: x /= y

      • Equivalent to: x = x / y

    • %= (Modulus Assignment):

      • Example: x %= y

      • Equivalent to: x = x % y

Comparison Operators

  • Description: Used to compare two values.

  • Operators and Examples:

    • == (Equality):

      • Example: x == 9

      • Result: false

    • === (Strict Equality) (checks both value and type):

      • Example: x === 5 (Result: true), x === "5" (Result: false)

    • != (Inequality):

      • Example: x != 9 (Result: true)

    • > (Greater Than):

      • Example: x > 9 (Result: false)

    • < (Less Than):

      • Example: x < 9 (Result: true)

    • >= (Greater Than or Equal To):

      • Example: x >= 9 (Result: false)

    • <= (Less Than or Equal To):

      • Example: x <= 9 (Result: true)

Logical Operators

  • Description: Used to perform logical operations.

  • Operators and Examples:

    • && (Logical AND):

      • Example: (x < 10 && y > 1) (Result: true)

    • || (Logical OR):

      • Example: (x == 6 || y < 1) (Result: false)

    • ! (Logical NOT):

      • Example: !(x == y) (Result: true)

  • Key Points:

    • Logical operators determine the logic between variables or values.

    • They can be applied to values of any type, not limited to Boolean.

    • The result can also be of any type.

    • Assumed Values: x = 5 and y = 3.