Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

JavaScript - Operators and Expressions

Operators and Expressions

Operators and expressions are essential components of JavaScript that allow you to perform various operations and manipulate data. Operators are symbols that represent actions or computations, while expressions are combinations of values, variables, and operators that produce a result. Here's an overview of operators and expressions in JavaScript:

Arithmetic Operators:

Arithmetic operators perform mathematical operations on numeric values.

  • Addition (+): Adds two numbers together.
  • Subtraction (-): Subtracts the right operand from the left operand.
  • Multiplication (*): Multiplies two numbers.
  • Division (/): Divides the left operand by the right operand.
  • Modulus (%): Returns the remainder of a division operation.
  • Exponentiation () (ES6):** Raises the left operand to the power of the right operand.
let x = 10;
let y = 5;

let additionResult = x + y; // 15
let multiplicationResult = x * y; // 50
let modulusResult = x % y; // 0
       

 

Assignment Operators:

Assignment operators 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 a = 5;
a += 2; // a is now 7
       

 

Comparison Operators:

Comparison operators compare values and return a Boolean result (true or false).

  • 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 type.
  • Strict Not Equal (!==): Checks if two values are not equal in either value or 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 p = 10;
let q = 5;

let isEqual = p === q; // false
let isGreaterThan = p > q; // true

 

Logical Operators:

Logical operators perform logical operations and return a Boolean result.

  • 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 Boolean value of the operand.
let isTrue = true;
let isFalse = false;

let result = isTrue && isFalse; // false