JavaScript Operators

What Are Operators?

Operators in JavaScript are symbols used to perform specific operations on one or more operands. They are essential for manipulating data and performing calculations. JavaScript includes several types of operators, such as arithmetic, string, assignment, and increment/decrement operators.

Types of Operators in JavaScript

Arithmetic Operators

  1. Used for mathematical operations.
  2. Examples: +, -, *, /, % (modulus)
  3. Example usage:

let sum = 5 + 3; // sum is 8

String Operators

  1. Used to concatenate strings.
  2. Example: + (concatenation)
  3. Example usage:

let greeting = "Hello, " + "world!"; // greeting is "Hello, world!"

Assignment Operators

  1. Used to assign values to variables.
  2. Examples: =, +=, -=
  3. Example usage:
let x = 5; // x is assigned the value 5
x += 3;    // x is now 8

Increment/Decrement Operators

  1. Used to increase or decrease a variable's value by 1.
  2. Examples: ++, --
  3. Postfix and prefix variations affect the timing of the operation.

Definition of Operators in JavaScript

Operators are fundamental components in JavaScript, used to perform various operations such as arithmetic, comparison, logical, assignment, and conditional operations. Each operator type has specific roles, enabling developers to create complex algorithms and control program flow.

Importance of Operators in Programming

Operators are crucial for computations, decision-making, and data manipulation in programming. They allow for efficient problem-solving and the development of robust software.

Arithmetic Operators

Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). They follow specific rules for precedence and associativity, ensuring the correct order of operations.

Addition Operator (+)

Used for adding numbers or concatenating strings.

Example:

let total = 5 + 3; // total is 8
let message = "Hello, " + "world!"; // message is "Hello, world!"

Subtraction Operator (-)

Used to subtract one number from another.

let difference = 5 - 3; // difference is 2

Multiplication Operator (*)

Used to multiply numbers.

let product = 5 * 3; // product is 15

Division Operator (/)

Used to divide one number by another.

Example:

let quotient = 10 / 2; // quotient is 5

Modulus Operator (%)

Returns the remainder of a division.

Example:

let remainder = 10 % 3; // remainder is 1

Comparison Operators

  1. Equal to (==)
    • Checks if values are equal (type conversion may occur).
    • Example: 5 == '5' // true
  2. Strict Equal to (===)
    • Checks if values and types are equal.
    • Example: 5 === '5' // false
  3. Not Equal to (!=)
    • Checks if values are not equal.
    • Example: 5 != '5' // false
  4. Strict Not Equal to (!==)
    • Checks if values and types are not equal.
    • Example: 5 !== '5' // true
  5. Less than (<)
    • Checks if the left value is less than the right value.
    • Example: 5 < 10 // true
  6. Greater than (>)
    • Checks if the left value is greater than the right value.
    • Example: 10 > 5 // true

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate