What are the basic JavaScript operators?

Basic JavaScript Operators

JavaScript operators are symbols that perform specific operations on one or more operands (values, variables, etc.) and return a result. These operators are essential for writing effective and efficient code in JavaScript. Let's explore the basic JavaScript operators:

Arithmetic Operators

Arithmetic operators are used to perform mathematical operations. The basic arithmetic operators in JavaScript are:

  1. Addition (+): Used to add two operands.
    Example: let sum = 5 + 3; // sum = 8

  2. Subtraction (-): Used to subtract the right operand from the left operand.
    Example: let difference = 10 - 4; // difference = 6

  3. Multiplication (*): Used to multiply two operands.
    Example: let product = 6 * 7; // product = 42

  4. Division (/): Used to divide the left operand by the right operand.
    Example: let quotient = 15 / 3; // quotient = 5

  5. Modulus (%): Used to get the remainder of the division of the left operand by the right operand.
    Example: let remainder = 17 % 5; // remainder = 2

  6. Increment (++): Used to increase the value of a variable by 1.
    Example: let x = 5; x++; // x = 6

  7. Decrement (--): Used to decrease the value of a variable by 1.
    Example: let y = 10; y--; // y = 9

Assignment Operators

Assignment operators are used to assign values to variables. The basic assignment operator is the equal sign (=), but there are also compound assignment operators that combine an arithmetic or logical operation with the assignment.

  1. Equal (=): Assigns the value of the right operand to the left operand.
    Example: let x = 5;

  2. Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.
    Example: let x = 5; x += 3; // x = 8

  3. Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.
    Example: let x = 10; x -= 4; // x = 6

  4. Multiplication Assignment (*=): Multiplies the left operand with the right operand and assigns the result to the left operand.
    Example: let x = 3; x *= 4; // x = 12

  5. Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.
    Example: let x = 10; x /= 2; // x = 5

  6. Modulus Assignment (%=): Divides the left operand by the right operand and assigns the remainder to the left operand.
    Example: let x = 7; x %= 3; // x = 1

Comparison Operators

Comparison operators are used to compare two values and return a boolean value (true or false).

  1. Equal (==): Checks if the two operands are equal in value.
    Example: let x = 5; let y = '5'; console.log(x == y); // true

  2. Strict Equal (===): Checks if the two operands are equal in value and type.
    Example: let x = 5; let y = '5'; console.log(x === y); // false

  3. Not Equal (!=): Checks if the two operands are not equal in value.
    Example: let x = 5; let y = 10; console.log(x != y); // true

  4. Strict Not Equal (!==): Checks if the two operands are not equal in value and type.
    Example: let x = 5; let y = '5'; console.log(x !== y); // true

  5. Greater Than (>): Checks if the left operand is greater than the right operand.
    Example: let x = 10; let y = 5; console.log(x > y); // true

  6. Less Than (<): Checks if the left operand is less than the right operand.
    Example: let x = 3; let y = 7; console.log(x < y); // true

  7. Greater Than or Equal To (>=): Checks if the left operand is greater than or equal to the right operand.
    Example: let x = 7; let y = 7; console.log(x >= y); // true

  8. Less Than or Equal To (<=): Checks if the left operand is less than or equal to the right operand.
    Example: let x = 5; let y = 10; console.log(x <= y); // true

Logical Operators

Logical operators are used to combine or invert boolean values.

  1. AND (&&): Returns true if both operands are true, otherwise, it returns false.
    Example: let x = 5; let y = 10; console.log(x > 3 && y > 7); // true

  2. OR (||): Returns true if at least one of the operands is true, otherwise, it returns false.
    Example: let x = 5; let y = 10; console.log(x > 7 || y > 7); // true

  3. NOT (!): Inverts the value of the operand. If the operand is true, it returns false, and if the operand is false, it returns true.
    Example: let x = true; console.log(!x); // false

Other Operators

  1. Conditional (Ternary) Operator (?:): Used as a shorthand for an if-else statement.
    Example: let age = 18; let canVote = age >= 18 ? 'Yes' : 'No'; // canVote = 'Yes'

  2. Typeof Operator: Returns the data type of the operand.
    Example: let x = 5; console.log(typeof x); // 'number'

  3. Comma Operator (,): Evaluates each operand from left to right and returns the value of the last operand.
    Example: let x = (5, 10); console.log(x); // 10

  4. Unary Operators: Operate on a single operand, such as increment (++), decrement (--), and the unary plus (+) and minus (-).
    Example: let x = 5; console.log(++x); // 6

Understanding these basic JavaScript operators will help you write more efficient and expressive code. Remember to practice and experiment with these operators to solidify your understanding.

0 Comments

no data
Be the first to share your comment!