JavaScript+Operators

JavaScript Operators

Overview

  • Operators in JavaScript are special symbols that perform operations on variables and values.

Arithmetic Operators

  • These operators are used to perform basic mathematical operations.

    • Operator Descriptions 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,

        • Example: X=5, Y=2; X/Y

        • Result: 2.5

      • % (Modulus):

        • Example: 5%2, 10%8, 10%2

        • Results: 1, 2, 0

      • ++ (Increment):

        • Example: X=5; X++

        • Result: X=6

      • -- (Decrement):

        • Example: X=5; X--

        • Result: X=4

Assignment Operators

  • These operators are used to assign values to variables.

    • Operator Examples (is the same as):

      • =

        • Example: x=y

      • +=

        • Example: x+=y

        • Equivalent to x= x + y

      • -=

        • Example: x-=y

        • Equivalent to x= x - y

      • *=

        • Example: x*=y

        • Equivalent to x= x * y

      • /=

        • Example: x/=y

        • Equivalent to x= x / y

      • %=

        • Example: x%=y

        • Equivalent to x= x % y

Comparison Operators

  • These operators are used to compare two values.

    • Operator Descriptions and Examples:

      • == (is equal to):

        • Example: x==9

        • Result: false

      • === (is exactly equal to):

        • Example: x==5 is true

        • Example: x=="5" is false

      • != (is not equal):

        • Example: x!=9 is true

      • > (is greater than):

        • Example: x>9 is false

      • < (is less than):

        • Example: x<9 is true

      • >= (is greater than or equal to):

        • Example: x>=9 is false

      • <= (is less than or equal to):

        • Example: x<=9 is true

    • Note: Assume x=5

Logical Operators

  • Logical operators are used to determine the logic between variables or values. They can operate on non-Boolean values as well.

    • Operator Descriptions and Examples:

      • **&& (AND