Operators

Operators

Operators are fundamental elements in JavaScript that enable you to perform various operations on data. They can be used to manipulate values, make comparisons, and perform other computations. In this section, we'll explore the different types of operators in JavaScript.

1. Arithmetic Operators

Arithmetic operators allow you to perform basic mathematical operations:

  • Addition (+): Adds two values together.

  • Subtraction (-): Subtracts the right operand from the left operand.

  • Multiplication (*): Multiplies two values.

  • Division (/): Divides the left operand by the right operand.

  • Modulus (%): Returns the remainder of the division.

  • Increment (++): Increases the value of a variable by 1.

  • Decrement (--): Decreases the value of a variable by 1.

let a = 5;
let b = 3;
let sum = a + b; // 8
let difference = a - b; // 2
let product = a * b; // 15
let quotient = a / b; // 1.6667
let remainder = a % b; // 2
let increment = a++; // a is now 6
let decrement = b--; // b is now 2

2. Comparison Operators

Comparison operators allow you to compare values and return a Boolean result:

  • Equal (==): Checks if two values are equal.

  • Not Equal (!=): Checks if two values are not equal.

  • Strict Equal (===): Checks if two values are equal in both value and data type.

  • Strict Not Equal (!==): Checks if two values are not equal in value or data type.

  • Greater Than (>): Checks if the left operand is greater than the right operand.

  • Less Than (<): Checks if the left operand is less than the right operand.

  • Greater Than or Equal (>=): Checks if the left operand is greater than or equal to the right operand.

  • Less Than or Equal (<=): Checks if the left operand is less than or equal to the right operand.

let x = 5;
let y = '5';
let isEqual = x == y; // true (values are equal)
let isStrictEqual = x === y; // false (types are different)
let isNotEqual = x != y; // false (values are equal)
let isStrictNotEqual = x !== y; // true (types are different)
let isGreaterThan = x > 3; // true
let isLessThan = x < 3; // false
let isGreaterOrEqual = x >= 5; // true
let isLessOrEqual = x <= 5; // true

3. Logical Operators

Logical operators are used to combine or manipulate Boolean values:

  • Logical AND (&&): Returns true if both operands are true.

  • Logical OR (||): Returns true if at least one operand is true.

  • Logical NOT (!): Returns the opposite of the operand's value.

let hasPermission = true;
let isLoggedIn = false;
let canAccessResource = hasPermission && isLoggedIn; // false
let isAuthorizedUser = hasPermission || isLoggedIn; // true
let isGuest = !isLoggedIn; // true

4. Assignment Operators

Assignment operators are used to assign values to variables:

  • Assignment (=): Assigns a value to a variable.

  • Addition Assignment (+=): Adds the right operand to the left operand and assigns the result to the left operand.

  • Subtraction Assignment (-=): Subtracts the right operand from the left operand and assigns the result to the left operand.

  • Multiplication Assignment (*=): Multiplies the left operand by the right operand and assigns the result to the left operand.

  • Division Assignment (/=): Divides the left operand by the right operand and assigns the result to the left operand.

let total = 10;
total += 5; // total is now 15
total -= 3; // total is now 12
total *= 2; // total is now 24
total /= 4; // total is now 6

5. Other Operators

JavaScript also has other operators, including:

  • Ternary (Conditional) Operator: A shorthand way to write simple conditional statements.
let age = 20;
let canVote = age >= 18 ? 'Yes' : 'No';
  • String Concatenation (+): Used to concatenate (combine) strings.
let firstName = 'John';
let lastName = 'Doe';
let fullName = firstName + ' ' + lastName; // 'John Doe'
  • Typeof Operator: Returns the data type of a value.
let x = 42;
console.log(typeof x); // 'number'

Understanding operators is crucial for performing various operations and comparisons in JavaScript. Operators help you manipulate and make decisions based on data, which is essential in programming.

In the next section, we'll delve into conditional statements and control structures to make more informed decisions in your code.