Cover Image for JavaScript Operators
277 views

JavaScript Operators

The javascript operators are used to perform operations on values and variables. There are several types of operators in JavaScript including arithmetic, comparison, logical, assignment, bitwise, and more.

JavaScript operators are symbols used to perform operations on variables and values. There are several types of operators in JavaScript, including:

1. Arithmetic Operators: Arithmetic operators are used to perform arithmetic operations on variables and values. Examples include + (addition), – (subtraction), * (multiplication), / (division), and % (modulus).

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
  • Modulus (%)

2. Assignment Operators: Assignment operators are used to assign values to variables. Examples include = (simple assignment), += (addition assignment), -= (subtraction assignment), *= (multiplication assignment), /= (division assignment), and %= (modulus assignment).

  • Assignment (=)
  • Addition assignment (+=)
  • Subtraction assignment (-=)
  • Multiplication assignment (*=)
  • Division assignment (/=)
  • Modulus assignment (%=)

3. Comparison Operators: Comparison operators are used to compare two values or variables. Examples include == (equality), != (inequality), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).

  • Equal to (==)
  • Not equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or equal to (>=)
  • Less than or equal to (<=)

4. Logical Operators: Logical operators are used to perform logical operations on variables and values. Examples include && (logical AND), || (logical OR), and ! (logical NOT).

  • And (&&)
  • Or (||)
  • Not (!)

5. Unary Operators: Unary operators are used to perform operations on a single variable or value. Examples include ++ (increment), — (decrement), and typeof (returns the data type of a variable).

  • unary minus(-)
  • increment(++)
  • decrement(- -)
  • NOT(!)
  • Addressof operator(&)
  • sizeof()

6. Ternary Operator: The ternary operator is a shorthand way of writing an if-else statement. It is denoted by ? : and is used to assign a value to a variable based on a condition.

JavaScript
let x = 10;
let y = 5;

// Arithmetic operators
console.log(x + y); // Output: 15
console.log(x - y); // Output: 5
console.log(x * y); // Output: 50
console.log(x / y); // Output: 2
console.log(x % y); // Output: 0

// Assignment operators
x += y; // Same as x = x + y
console.log(x); // Output: 15

// Comparison operators
console.log(x == y); // Output: false
console.log(x < y); // Output: false
console.log(x > y); // Output: true

// Logical operators
console.log(x > y && x < 20); // Output: true
console.log(x == y || x > 20); // Output: true

// Unary operators
console.log(typeof x); // Output: number
console.log(++y); // Output: 6

// Ternary operator
let z = x > y ? 'x is greater than y' : 'y is greater than or equal to x';
console.log(z); // Output: x is greater than y

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS