4javascriptoperators

JavaScript Operators

This guide introduces operators in JavaScript and how they help us work with variables and values. Operators make JavaScript powerful by helping us work with values in different ways. Try using these operators to see what they can do!

Types of JavaScript Operators

Operators perform tasks like math operations and comparisons. Here are some main types:

1. Arithmetic Operators

These do math operations:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • % (modulus, remainder)
let x = 10;
let y = 5;

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

2. Comparison Operators

These operators compare values and return true or false:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)
let a = 10;
let b = 20;

console.log(a == b); // Output: false
console.log(a != b); // Output: true
console.log(a < b);  // Output: true

3. Logical Operators

Logical operators combine multiple conditions:

  • && (AND) - true if both conditions are true
  • || (OR) - true if at least one condition is true
  • ! (NOT) - inverts true to false and false to true
let hasLicense = true;
let hasCar = false;

console.log(hasLicense && hasCar); // Output: false
console.log(hasLicense || hasCar); // Output: true
console.log(!hasCar); // Output: true

Note: We aim to make learning easier by sharing top-quality tutorials, but please remember that tutorials may not be 100% accurate, as occasional mistakes can happen. Once you've mastered the language, we highly recommend consulting the official documentation to stay updated with the latest changes. If you spot any errors, please feel free to report them to help us improve.

top-home