MakeW3web

JavaScript Operators

JavaScript operators are symbols used to perform operations on values and variables. They are the building blocks for creating more complex expressions and logic in your code. Here's an overview of the most important JavaScript operators, grouped by category:

  1. Arithmetic operators:

Arithmetic operators perform basic mathematical operations, such as addition, subtraction, multiplication, and division.

  • `+`: Addition
  • `-`: Subtraction
  • `*`: Multiplication
  • `/`: Division
  • `%`: Modulus (remainder)
  • `**`: Exponentiation (ES6)
  • `++`: Increment (increases the value by 1)
  • `--`: Decrement (decreases the value by 1)
var a = 10 + 5; // 15
var b = 10 - 5; // 5
var c = 10 * 2; // 20
var d = 10 / 2; // 5
var e = 10 % 3; // 1
var f = 2 ** 3; // 8
  1. Assignment operators:

Assignment operators are used to assign values to variables.

  • `=`: Assign
  • `+=`: Add and assign
  • `-=`: Subtract and assign
  • `*=`: Multiply and assign
  • `/=`: Divide and assign
  • `%=`: Modulus and assign
  • `**=`: Exponentiation and assign (ES6)
var a = 10;
a += 5; // a = a + 5; a is now 15
a -= 5; // a = a - 5; a is now 10
a *= 2; // a = a * 2; a is now 20
a /= 2; // a = a / 2; a is now 10
a %= 3; // a = a % 3; a is now 1
a **= 2; // a = a ** 2; a is now 1
  1. Comparison operators:

Comparison operators are used to compare two values and return a boolean value that indicates whether the comparison is true or false.

  • `==`: Equal (loose equality, allows type coercion)
  • `===`: Strictly equal (strict equality, requires the same data type)
  • `!=`: Not equal (loose inequality)
  • `!==`: Strictly not equal (strict inequality)
  • `<`: Less than
  • `>`: Greater than
  • `<=`: Less than or equal to
  • `>=`: Greater than or equal to
var a = 10;
var b = "10";

console.log(a == b);  // true (loose equality allows type coercion)
console.log(a === b); // false (strict equality requires the same data type)
console.log(a != b);  // false
console.log(a !== b); // true

console.log(a < 20);  // true
console.log(a > 20);  // false
console.log(a <= 10); // true
console.log(a >= 10); // true
  1. Logical operators:

Logical operators are used to perform logical operations, such as AND, OR, and NOT. They are commonly used in conditional statements.

  • `&&`: Logical AND
  • `||`: Logical OR
  • `!`: Logical NOT
var a = true;
var b = false;

console.log(a && b); // false
console.log(a || b); // true
console.log(!a);     // false
console.log(!b);     // true
  1. Other operators:
  • `typeof`: Returns a string representing the data type of a value or variable.
console.log(typeof 42); // "number"
console.log(typeof "Hello, World!"); // "string"

`instanceof`: Tests whether an object is an instance of a specific constructor or class.

var arr = [1, 2, 3];
console.log(arr instanceof Array); // true
console.log(arr instanceof Object); // true

  • `in`: Checks if an object has a specific property.
var obj = { key: 'value' };
console.log('key' in obj); // true
console.log('nonexistent' in obj); // false

  • `delete`: Removes a property from an object.
var obj = { key: 'value' };
delete obj.key;
console.log('key' in obj); // false

  • `?:`: Ternary operator (conditional operator), a shorthand way to write an if-else statement. It takes three operands: a condition, a value to return if the condition is true, and a value to return if the condition is false.
var a = 10;
var b = 20;
var max = a > b ? a : b; // max will be 20

These are the main JavaScript operators, which allow you to perform various operations on values and variables, such as arithmetic, assignment, comparison, logical operations, and more. Understanding and utilizing these operators is essential for creating complex expressions and logic in your JavaScript code.

Was this page helpful?