1 00:00:00,280 --> 00:00:03,055 The ternary operator a is a very compact, 2 00:00:03,055 --> 00:00:07,670 two-way branch that many programmers use to write very concise code. 3 00:00:09,010 --> 00:00:13,580 The only problem is, is that it's so compact it can be hard to read. 4 00:00:13,580 --> 00:00:17,630 It's called ternary because it involves three expressions. 5 00:00:17,630 --> 00:00:23,650 The first is a boolean that determines which of the next two expressions run. 6 00:00:23,650 --> 00:00:29,300 The ternary operator uses two characters, a question mark and a colon. 7 00:00:29,300 --> 00:00:34,030 These two characters are markers for our condition and our two branches. 8 00:00:35,050 --> 00:00:38,530 Let's see how this works in our two.js file from earlier. 9 00:00:39,550 --> 00:00:46,060 First, let's write our ternary operator, a question mark and a colon. 10 00:00:46,060 --> 00:00:50,270 I'll show you how this works just by copying/pasting from this 11 00:00:50,270 --> 00:00:51,330 if statement above. 12 00:00:53,350 --> 00:00:57,900 Let's take the condition, which is stored in the variable isTrue, 13 00:00:57,900 --> 00:00:59,730 and paste it before the question mark. 14 00:01:00,882 --> 00:01:07,130 If isTrue evaluates to true, then the code after the question mark will run. 15 00:01:09,631 --> 00:01:12,890 If not, the code after the colon will run. 16 00:01:13,930 --> 00:01:19,630 So let's copy and paste the true branch, and 17 00:01:19,630 --> 00:01:21,910 paste it just after the question mark here. 18 00:01:25,863 --> 00:01:29,290 And copy and paste the full branch here at the end. 19 00:01:30,770 --> 00:01:33,560 Don't forget to terminate your statement with a semicolon. 20 00:01:34,680 --> 00:01:37,990 Now, if we comment out our if else statement, 21 00:01:39,540 --> 00:01:41,930 we can see the ternary operator in action. 22 00:01:46,116 --> 00:01:53,040 If we run node two.js in the command line, we get yes log. 23 00:01:53,040 --> 00:01:56,784 If we change isTrue to false and 24 00:01:56,784 --> 00:02:00,966 run it again, we see no is logged. 25 00:02:00,966 --> 00:02:05,872 You can see that the ternary expression is almost exactly like 26 00:02:05,872 --> 00:02:10,040 an if else statement, but with a bare bones syntax. 27 00:02:10,040 --> 00:02:15,040 But there is another way you're likely to see a ternary pop up in the wild. 28 00:02:15,040 --> 00:02:15,820 As an expression. 29 00:02:16,910 --> 00:02:21,160 In JavaScript and expression is something that resolves to a value. 30 00:02:21,160 --> 00:02:26,990 You can assign an expression to a variable and that variable will contain the value. 31 00:02:26,990 --> 00:02:34,004 For example, you can assign var x to equal 2 + 5. 32 00:02:34,004 --> 00:02:40,570 2 + 5 is an expression that resolves to the value of 7. 33 00:02:40,570 --> 00:02:43,090 X isn't going to hold 2 + 5, 34 00:02:43,090 --> 00:02:47,820 it's going to hold the value that the expression returns, which is 7. 35 00:02:47,820 --> 00:02:52,420 In the same way, a ternary expression will return one of it's 36 00:02:52,420 --> 00:02:57,250 two branches as a value, depending on the truthiness of the first operand. 37 00:02:58,350 --> 00:03:02,210 Let's see this in the code to get a better idea. 38 00:03:02,210 --> 00:03:06,720 Instead of logging yes or no to the console directly from the ternary, 39 00:03:06,720 --> 00:03:11,425 let's return one of those values and store it in a variable called yesOrNo. 40 00:03:22,458 --> 00:03:25,791 Then we can log that variable to the console, 41 00:03:30,132 --> 00:03:34,287 When the ternary is a evaluated by the interpreter, 42 00:03:34,287 --> 00:03:37,026 it examines the value of isTrue and 43 00:03:37,026 --> 00:03:42,040 returns the first value of yes if it's true, and no if it i not. 44 00:03:43,640 --> 00:03:46,765 And it's choice will be stored in yesOrNo. 45 00:03:48,190 --> 00:03:50,830 Let's save it and run it again. 46 00:03:52,130 --> 00:03:57,790 Now let's change it to true and run the script again. 47 00:04:00,190 --> 00:04:05,350 Technically, you could use the ternary for a multi-way branching while putting 48 00:04:05,350 --> 00:04:10,730 one expression as the condition for the larger ternary expression. 49 00:04:10,730 --> 00:04:15,910 But this gets messy real quick, and it's not a recommended practice. 50 00:04:15,910 --> 00:04:20,250 If you ever use the ternary expression, it should probably be for 51 00:04:20,250 --> 00:04:25,410 very simple cases where the intent is unmistakable and obvious. 52 00:04:25,410 --> 00:04:29,310 For our last conditional, let's have a look at short circuiting. 53 00:04:29,310 --> 00:04:30,460 I'll see you in the next video.