Javascript

Operators in JavaScript are symbols that perform operations on operands (variables, values, or expressions). They are classified into different categories based on their functionality. Here's an overview of the main types of operators in JavaScript:

  1. Arithmetic Operators:

    • Arithmetic operators perform mathematical operations on numeric operands.
    javascript
    var a = 10; var b = 5; var sum = a + b; // Addition var difference = a - b; // Subtraction var product = a * b; // Multiplication var quotient = a / b; // Division var remainder = a % b; // Modulus var exponentiation = a ** b; // Exponentiation (ES7)
  2. Assignment Operators:

    • Assignment operators are used to assign values to variables.
    javascript
    var x = 10; // Assign value 10 to variable x var y = 5; y += 3; // Equivalent to: y = y + 3 (Addition assignment)
  3. Comparison Operators:

    • Comparison operators compare two values and return a boolean result (true or false).
    javascript
    var a = 10; var b = 5; var isEqual = a == b; // Equal to var isNotEqual = a != b; // Not equal to var isGreater = a > b; // Greater than var isLess = a < b; // Less than var isGreaterOrEqual = a >= b; // Greater than or equal to var isLessOrEqual = a <= b; // Less than or equal to
  4. Logical Operators:

    • Logical operators are used to combine or manipulate boolean values.
    javascript
    var x = true; var y = false; var andResult = x && y; // Logical AND var orResult = x || y; // Logical OR var notResult = !x; // Logical NOT
  5. Unary Operators:

    • Unary operators are used with a single operand.
    javascript
    var x = 10; var y = -x; // Unary negation (-) var z = ++x; // Increment (prefix) var w = x--; // Decrement (postfix)
  6. String Concatenation Operator:

    • The + operator can also be used to concatenate strings.
    javascript
    var firstName = "John"; var lastName = "Doe"; var fullName = firstName + " " + lastName; // Concatenation
  7. Conditional (Ternary) Operator:

    • The conditional operator is a shorthand for an if-else statement.
    javascript
    var age = 20; var status = (age >= 18) ? "Adult" : "Minor"; // Ternary operator

Operators are fundamental to JavaScript programming, allowing you to perform various operations and make decisions in your code. Understanding how to use operators effectively is essential for writing efficient and concise JavaScript programs.

Thanks for learning