Loading...
Loading...
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
0 Interaction
991 Views
Views
42 Likes
×
×
🍪 CookieConsent@Ptutorials:~

Welcome to Ptutorials

Note: We aim to make learning easier by sharing top-quality tutorials.

We kindly ask that you refrain from posting interactions unrelated to web development, such as political, sports, or other non-web-related content. Please be respectful and interact with other members in a friendly manner. By participating in discussions and providing valuable answers, you can earn points and level up your profile.

$ Allow cookies on this site ? (y/n)

top-home