
Join the Conversation!
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
In this lesson, you'll learn about logical operators in JavaScript, focusing on more complex examples and understanding their behavior in different contexts.
Logical operators are used to combine two or more conditions. If you remember correctly, JavaScript includes three logical operators: (OR), (AND), (NOT).
The double ampersand is known as the AND operator. It checks whether two operands are truthy values. If they are truthy, it returns ; otherwise, it returns . They are often used as a condition in an if statement. Let's show that in an example.
Let's say that we want to choose which people may enter our club. To enter, they need to be cool, and they also need to be older than 18.
const age = 19; // age is represented by a number
const isCool = true; // isCool is represented by a boolean
if (isCool && age > 18) { // notice how we didn't say isCool === true
console.log('You may enter.');
} else {
console.log('You cannot enter.');
}
Now, the point of this lecture is not an statement. Let's remove that so we can focus purely on the logical operators. The only thing that
const age = 19;
const isCool = true;
console.log(isCool && age > 18);
We got , which is not a surprise. But now, instead of these true boolean values, let's test it with some truthy values.
console.log('truthy' && 1 && 'test' && 999);
The output of this is , and why is that so? Shouldn't the logical operator return a boolean value? Here's how it works.
The AND operator does the following:
Now you know why was returned; all the values were truthy, and it was the last one on the list.
What if we change one value to be falsy?
console.log('truthy' && 0 && 'test' && 999);
As you can see, if even one falsy value exists, it's going to stop and immediately return that value.
In other words, AND returns the first falsy value or the last truthy value if no falsy values have been found.
The syntax for the OR operator is two straight vertical lines . It checks whether any one of the operands is a truthy value.
Let's see it in action:
console.log('truthy' || 0 || 'test' || 999);
We get . Why is that? Well, let's see how the OR operator works:
The OR operator does the following:
In other words, a chain of OR returns the first truthy value or the last one if no truthy value is found.
So now, if we change all of the values to be falsy, it is going to return the last one:
console.log('' || 0 || null || undefined);
As you can see, we get .
JavaScript is lazy. It will want to do the least amount of work possible to get its return value.
With the operator: JavaScript will first try to return the first falsy value. If none were found, it will return the last truthy value.
And with the operator: JavaScript will first try to return the first truthy value. If none were found, it will return the last falsy value.
"Please login to view comments"
Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.
How did you manage to remove the blur property and reach here?
Upgrading gives you access to quizzes so you can test your knowledge, track progress, and improve your skills.
By logging in, you'll unlock full access to this and other free tutorials on JSM Pro.
Why? Logging in lets us personalize your learning experience, track your progress, and keep you in the loop with new workshops, coding tips, and platform updates.
You'll also be the first to know about upcoming launches, events, and exclusive discounts.
No spam—just helpful content to level up your skills.
If that sounds fair, go ahead and log in to continue →
Enter your name and email to get instant access
##Looks like we found a thief monkey By the way, I liked the trick how you reached till here. You have a good sense of humor. You will improve a lot if you join our course with this passion.
var
(function-scoped, outdated)let
(block-scoped, modern and recommended)const
(block-scoped, cannot be reassigned)_
, or $
let let = 5;
is invalid)myVar
and myvar
are different)string
, number
, boolean
, null
, undefined
, bigint
, symbol
Objects
, Arrays
, Functions
Subscribing gives you access to a brief, insightful summary of each lecture to stay on track.
00:00:00 I call this lesson Logical Operators Part 2, because you and I already talked about the three logical operators in the JavaScript language.
00:00:08 The OR, the AND, and the NOT.
00:00:11 But now, we're going to dive deeper.
00:00:13 Let's start with the AND operator.
00:00:15 As you might already know, the AND operator checks whether two operands are truthy values.
00:00:21 If they are truthy, it returns true.
00:00:23 Otherwise, it returns false.
00:00:25 And these are often used within if statements.
00:00:28 So, now that you know how to form an if statement, let me give you an example.
00:00:32 I'll define a variable of age of something like 19. And I'll also define another variable of something like isCool.
00:00:42 And I'll make that equal to a boolean value of true.
00:00:45 Now, I'll do an if statement and check if age is greater than 18. and is cool, and then if that is the case, I will consulog you may enter,
00:01:00 else we can do a consulog and say you cannot enter.
00:01:06 Obviously, in this case, we get you may enter because age is 19 and is cool as true.
00:01:13 But wait, notice how we didn't explicitly say is cool is triple equal to true.
00:01:20 Don't get me wrong, this would also be okay, but it is not needed.
00:01:25 Why?
00:01:26 Because isCool is considered a truity value.
00:01:29 You know that, right?
00:01:30 So with proper naming, such as isCool, this triple equality becomes redundant.
00:01:36 You don't have to add it.
00:01:37 You can just say if age is greater than 18 and isCool.
00:01:41 because isCool itself is considered a truthy value.
00:01:45 Now, the point of this lesson is not an if and else statement.
00:01:49 So let's remove it so we can focus purely on the logical operators.
00:01:54 The only thing we're going to keep is the condition.
00:01:57 So let's log it to the console.
00:02:00 I'll say console.log and just put this entire condition.
00:02:04 If you save it, you'll see that the output of this condition is true, which is not a surprise, right?
00:02:11 Because the AND operator checks two operands, and if both are truthy, which they are, it returns true.
00:02:19 But this makes full sense, right?
00:02:20 Because both of these are true Boolean values.
00:02:24 is cool is obviously a boolean true and a comparison always returns a real boolean true.
00:02:31 So basically we have true and true, which with an end operator in between returns what?
00:02:39 Well, true.
00:02:40 That makes sense.
00:02:41 But now, instead of these true boolean values, let's try it with some truthy values, okay?
00:02:47 For example, we can pass in a string of truthy.
00:02:52 And we can maybe chain a number 1. And we can chain a string of test, for example.
00:02:58 And we can chain maybe like another number.
00:03:01 What do you think the output of this will be?
00:03:03 The output is 999. But why is that the case?
00:03:07 Shouldn't the logical operator AND return a Boolean value?
00:03:11 Well, here's how it works.
00:03:12 The AND operator works in the following way.
00:03:16 It evaluates operands from left to right, starting with truthy, 1, test, and then 999. it converts them to a Boolean value.
00:03:27 If the result is true, it continues to the next value.
00:03:30 And if it is false, it stops and returns the original value of that operand.
00:03:35 But if all operands have been evaluated to true, then it returns the last operand.
00:03:41 So now you know why the last number was returned.
00:03:44 because all the values were truthy and it was the last one on the list.
00:03:48 But what if we change one value to be falsy?
00:03:51 Like, instead of 1, we can put 0 right here.
00:03:54 It'll make a comparison between truthy and 0, and for the AND operator, both of these need to be truthy for it to return true,
00:04:02 but in this case, 0 is falsy, so it'll stop the comparison right here and return the 0. So as you can see, if there's at least one falsy value,
00:04:12 it's going to stop and immediately return that value.
00:04:15 In other words, simply explained and returns the first falsy value it encounters or the last truthy value if no falsy values have been found.
00:04:26 You can go ahead and test it out a bit to fully understand what the output will be.
00:04:31 Only when you can change a couple of values and fully understand what the output is, then you know how it works.
00:04:37 But now let's dive into the second operator in the list, which is the OR operator.
00:04:42 The OR operator checks whether any of the operands is a truthy value.
00:04:47 In action, that would look something like this.
00:04:50 console.log truthy OR 0 OR test OR 999, for example.
00:05:00 What do you think the output will be?
00:05:03 Well, if any is truthy, right?
00:05:06 Here we have multiple.
00:05:07 Truthy, test, and 9 and 9 are all truthy.
00:05:10 So it'll return the first truthy value that matches.
00:05:14 Why is that?
00:05:16 Well, let me show you how the OR operator works under the hood.
00:05:19 Same as the AND operator, it starts from the left.
00:05:23 For each operand, it converts it into a Boolean.
00:05:26 If the result is true, it immediately stops the entire execution and returns the original value of that boolean, which is exactly what happened here.
00:05:35 But if it is false, for example, it'll continue to the second one and try to test that one.
00:05:41 But if all operands have been evaluated to falsy, like in this case, it'll return the last falsy one because it couldn't find any of the truthy ones.
00:05:52 In simple words, OR will return the first truthy value or the last one if no truthy value is found.
00:06:01 And let's clean it up a bit and bring back the explanation for the AND, just so we have it right here.
00:06:06 And we are ready to discuss the last of the three JavaScript logical operators.
00:06:11 And that is the NOT operator.
00:06:13 It reverses the Boolean result of a condition.
00:06:15 The syntax is pretty simple and you know it already.
00:06:19 If NOT true, then obviously it'll give us false.
00:06:24 This operator accepts a single argument and then converts it to a boolean type, so it returns the inverse value.
00:06:32 If true, return false.
00:06:34 If not false, it is obviously true.
00:06:36 But here's a pretty cool thing.
00:06:38 It doesn't just work in booleans.
00:06:40 it works on truthy and falsey values too.
00:06:43 So if you say not and then pass some kind of a falsey values, so not zero, the result is true.
00:06:51 Because not falsey is truthy.
00:06:54 In a similar way, if you pass a truthy value, so for example, a string of test, not truthy is obviously false.
00:07:04 Obviously, this builds up on the previous lesson of truthy and falsey values.
00:07:08 And interestingly enough, this not operator allows us an even simpler way to figure out whether a value is truthy or falsey.
00:07:16 So let's explore that and a third way of figuring out truthy or falsey values in JavaScript in the next lesson.