JavaScript fundamentals: operators
- Comparison Operators
- Relational Operators
- Logical Operators
Comparison Operators
We have two types:
a. Strict Operators
// === Strict equality operator
'7' === 7
//false
// !== strict inequality operator
'7' !== 7
//trueb. Loose Operators
// === loose equality operator
'7' == 7
//true
// !== loose inequality operator
'7' !== 7
//falseRelational Operators
let x = 5;
//Greater than >
x > 1;
//true
// Greater than or equal to >=
x >= 5;
//true
//Less than <
x < 5;
//false
//Less than or equal to <=
x <=5;
//true
Logical Operators
Get Patricia Njoroge’s stories in your inbox
Join Medium for free to get updates from this writer.
Logical AND (&&):
The logical AND operator (`&&`) in JavaScript evaluates two expressions and returns the value of the first expression if it is falsey, otherwise, it returns the value of the second expression. Here are a couple of examples:
let x = 5;
let y = 10;
let result = (x < 10 && y > 5);
console.log(result); // Output: trueIn this example, the expression `(x < 10 && y > 5)` evaluates to `true` because both conditions are true. Therefore, the value of the second expression (`y > 5`) is returned.
Logical OR (||):
The logical OR operator (`||`) in JavaScript evaluates two expressions and returns the value of the first expression if it is truthy, otherwise, it returns the value of the second expression. Let’s look at a couple of examples:
let age = 25;
let isAdult = (age >= 18 || age <= 65);
console.log(isAdult); // Output: trueIn this example, the expression `(age >= 18 || age <= 65)` evaluates to `true` because the first condition (`age >= 18`) is true. As a result, the value of the first expression (`age >= 18`) is returned.
Logical NOT (!):
The logical NOT operator (`!`) in JavaScript negates the truthiness of an expression. It returns `false` if the expression is truthy and `true` if the expression is falsey. Let’s see some examples:
let sunnyDay = true;
let isRaining = !sunnyDay;
console.log(isRaining); // Output: falseIn this example, the expression `!sunnyDay` negates the truthiness of `sunnyDay` (which is `true`), so the result is `false`.
These logical operators are handy tools for controlling the flow of your code and making decisions based on specific conditions. Understanding their behavior is crucial for writing effective and expressive JavaScript code.








