
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 the in JavaScript, a concise way to perform simple checks.
You could say that the statement is a more complex version of the statement. There's yet another version of it: the ternary operator.
It should be used just for simple checks.
To explain the ternary operator , let's first take a look at the syntax of a typical statement:
if (condition) {
value if true
} else {
value if false
}
Now, the ternary operator:
condition ? value if true : value if false
Although this is just pseudocode, meaning the code is written in half English and half real syntax, I think you can still see how we would use the . Let's use the same old driver's license example we had when we were learning about the statement .
if (person.age > 18) {
console.log('You can drive');
} else {
console.log('You may not drive yet');
}
And now let's transfer it to a . I'll first write it out and then we're going to explore it in detail:
person.age > 18 ? console.log('You can drive') : console.log('You may not drive yet');
At first, may seem a bit weird and hard to read. But as you write more of them, you'll quickly get better at understanding them. They'll quickly become your go-to tool if you have just a simple or question.
"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 You could say that the switch statement is a more complex version of the if statement.
00:00:06 But there's yet another version of it, the ternary operator.
00:00:10 You can think of it as an even simpler version of an if statement.
00:00:14 It is used just for simple true or false checks.
00:00:17 To explain the ternary operator, I'll first write down the syntax of a typical if statement.
00:00:23 We have a block of code that will be executed if a value is true and a block of code that will be executed if a value is false.
00:00:30 Now, the syntax of a ternary operator looks something like this.
00:00:35 See?
00:00:36 It has the same things.
00:00:37 The condition, value if true, and value if false.
00:00:41 Although this is just pseudocode, meaning the code that is written in half English and half real syntax, I think you can still see how we would use the
00:00:50 ternary operator.
00:00:51 Let's use a driver's license example.
00:00:53 We can have an age of something like 25, and then if age is greater than 18, We're going to run a console.log of, you can drive.
00:01:06 Else, console.log, you may not drive yet.
00:01:12 Of course, 25 is greater than 18, so we can drive.
00:01:16 Now let's transfer it over to a ternary.
00:01:20 I'll first write it out and then I'll explain it in detail.
00:01:23 Age is greater than 18, question mark, console.log, you can drive.
00:01:32 colon, you may not drive yet.
00:01:36 This is how it looks like.
00:01:38 Oh, and let's not forget to console log this statement as well.
00:01:42 Now you can see that we have the same exact output.
00:01:45 So the way in which it works is that reading from left to right, we first have our condition.
00:01:51 Then we have a question mark.
00:01:54 with the code that will be executed if the condition is true.
00:01:58 Finally, following the colon sign, we have the code that will be executed if a condition is false.
00:02:06 This might seem a bit weird and hard to read at first, but as you write more of them, you'll quickly understand how they work.
00:02:12 And then, ternaries will quickly become your go-to tool if you just have a simple true or false question.
00:02:19 You can also write them like this by putting them in multiple lines, keeping condition at the top, and then have two new lines,
00:02:26 one for each of the cases.
00:02:28 Now, if something is repeated in both of these cases, such as a console log in this case, you can actually wrap the entire thing within a console log and
00:02:39 just return the strings.
00:02:41 That would look something like this.
00:02:43 console log, and then if age is greater than 18, return this, else return this.
00:02:50 You can see that once again, we get the same output, but check this out.
00:02:55 Now it fits in a single, simple to understand line.
00:02:59 Isn't that beautiful?
00:03:00 Much simpler than having to write the entire if and else blocks.
00:03:04 I'm telling you, you'll use this one quite a lot.