javascript
  1. javascript-operators

JavaScript Operators

Syntax

JavaScript operators are used to perform operations on operands and return a value. In general, an operator works on one or more operands and returns a result.

operand operator operand

Example

let x = 5;
let y = 3;

console.log(x + y); // Output: 8
console.log(x - y); // Output: 2
console.log(x * y); // Output: 15
console.log(x / y); // Output: 1.6666666666666667
console.log(x % y); // Output: 2
console.log(x ** y); // Output: 125
console.log(x < y); // Output: false
console.log(x > y); // Output: true
console.log(x <= y); // Output: false
console.log(x >= y); // Output: true
console.log(x == y); // Output: false
console.log(x != y); // Output: true
console.log(x === y); // Output: false
console.log(x !== y); // Output: true

Output

The output of the above example would be:

8
2
15
1.6666666666666667
2
125
false
true
false
true
false
true
false
true

Explanation

There are several types of operators in JavaScript. Below are the brief definitions for each type:

  • Arithmetic Operators: These operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, modulus, and exponentiation. Examples: +, -, *, /, %, **

  • Assignment Operators: These operators are used to assign values to variables. Examples: =, +=, -=, *=, /=, %=.

  • Comparison Operators: These operators are used to compare two values and return a Boolean value. Examples: <, >, <=, >=, ==, !=, ===, !==.

  • Logical Operators: These operators are used to perform logical operations such as AND, OR, and NOT and return a Boolean value. Examples: &&, ||, !.

  • Bitwise Operators: These operators are used to perform bitwise operations such as bitwise AND, OR, XOR, and NOT and return an integer value. Examples: &, |, ^, ~, <<, >>, >>>.

  • Conditional Operators: These operators are used to assign values based on a condition. Example: condition ? value1 : value2.

  • Type Operators: These operators are used to determine the type of a value. Example: typeof, instanceof.

Use

Operators are used extensively in JavaScript to perform complex operations on values, such as mathematical calculations, logical evaluations, and comparisons. Understanding how to use operators, and which operator to use, is essential for writing efficient and effective JavaScript code.

Important Points

  • JavaScript operators are used to perform operations on operands and return a value.
  • There are several types of operators in JavaScript, including arithmetic, assignment, comparison, logical, bitwise, conditional, and type operators.
  • Understanding how to use operators, and which operator to use, is essential for writing efficient and effective JavaScript code.

Summary

JavaScript operators are an essential part of the language and are used to perform operations on operands and return a value. The different types of operators, including arithmetic, assignment, comparison, logical, bitwise, conditional, and type operators, provide a range of functionality for performing complex operations on values. Understanding how to use operators, and which operator to use, is essential for writing efficient and effective JavaScript code.

Published on: