Bite-sized JavaScript: Comparison and Logical Operators

Welcome back to another episode of "Fun with JavaScript!" Today we're going to take a look at comparison and logical operators, and I promise to make it as entertaining as possible.

Bite-sized JavaScript: Comparison and Logical Operators
Photo by Carnivore Style / Unsplash

Welcome back to another episode of "Fun with JavaScript!" Today we're going to take a look at comparison and logical operators, and I promise to make it as entertaining as possible.

Comparison operators are used to compare two values, and the result is always a boolean value (true or false). Here are some examples:

let x = 5;
let y = 10;

console.log(x > y); // false
console.log(x < y); // true
console.log(x >= y); // false
console.log(x <= y); // true
console.log(x == y); // false
console.log(x != y); // true

Now, let's move on to logical operators. These are used to combine two or more conditions, and the result is also a boolean value. Here are the most commonly used ones:

  • && (and) - both conditions must be true
  • || (or) - at least one condition must be true
  • ! (not) - negates the result of a condition

Let's see them in action:

let age = 25;
let isStudent = true;

console.log(age > 18 && isStudent); // true
console.log(age > 30 || isStudent); // true
console.log(!isStudent); // false

As you can see, JavaScript is quite good at making sense of logical expressions. It's up to you to decide how to use them in your code, but keep in mind that readability and clarity are key.

In conclusion, don't be afraid to play around with comparison and logical operators in JavaScript. Who knows, you might even find them amusing! And if you don't, at least you'll have a good reason to laugh at my jokes. Cheers!