From the course: Learning the JavaScript Language

Unlock the full course today

Join today to access over 24,500 courses taught by industry experts.

Terse ifs: Ternaries

Terse ifs: Ternaries

- [Instructor] In this video, we'll look at one more quite terse if construct, which is called the ternary operator or the conditional operator. It's an expression of some kind followed by a question mark, followed by an action to take if that expression evaluates to true, then a colon, and then the other action to take if the expression evaluates to false. So I'm going to start with a variable to give me something to play with here. We'll create this animal that is a cat, and now I'll write out an expression using this ternary operator. So my expression will be if the animal is a cat. Actually, let's go into the editor mode 'cause I think it would be better to spread this across a couple of lines. So if the animal is a cat, and then I'm going to add in a question mark. And if it is, we'll log a message to the console that says "You will be a cat herder." And if this is false, we'll log a second message. "You will be…

Contents