DEV Community

Hiral
Hiral

Posted on

JavaScript Operators: The Basics You Need to Know

Operators are symbols that tells computer to take action:

Arithmetic operators

let a = 10;
let b = 3;

console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.333...
console.log(a % b); // 1 //gives remainder after division
Enter fullscreen mode Exit fullscreen mode

Comparison Operators

 == : Equal (loose) eg. 5 =="5" will return true
 ===: strict equal eg. 5=="5" will return false
 !=: not equal eg. 5!=3 will return true
 >: greater than 5>3 will return true
 < :less than 5<3 will return true
Enter fullscreen mode Exit fullscreen mode

Logical Operators

&& : AND
|| : OR
! : Not
Enter fullscreen mode Exit fullscreen mode

Truth Table( &&):

  • True && True = True
  • True && False = False
  • False && True = False
  • false && false = false

Truth table(||):

  • True || true = true
  • true || false = true
  • false || true = true
  • false || false = false

Truth Table : (!)

  • true =false
  • false = true

example

let age = 20;
let hasID = true;

console.log(age > 18 && hasID); // true
console.log(age < 18 || hasID); // true
console.log(!hasID); // false
Enter fullscreen mode Exit fullscreen mode

Top comments (0)