- Overview
- Transcript
4.1 Conditionals
What happens when we need to do one thing if a condition is true, or something else if it isn’t? We use an if statement, that’s what, and in this lesson we’ll learn all about if statements and the ternary operator.
Syntax
if
else
() ? :
1.Introduction2 lessons, 07:42
1.1Introduction02:12
1.2Setup05:30
2.Language Fundamentals8 lessons, 1:00:53
2.1Variables06:33
2.2Data Types11:28
2.3Arithmetic, Assignment, and Comparison Operators10:24
2.4Unary, Logical, Comma, and Spread Operators09:02
2.5Operator Precedence03:50
2.6Reserved Words04:17
2.7Strict Mode04:34
2.8Functions10:45
3.Data Structures5 lessons, 22:52
3.1Arrays04:29
3.2Objects04:30
3.3Sets04:57
3.4Maps04:21
3.5Weak Maps and Weak Sets04:35
4.Controlling Program Execution7 lessons, 37:06
4.1Conditionals07:49
4.2Switch Statements04:41
4.3The For Loop06:39
4.4The `for .. in` Loop05:17
4.5The `for .. of` Loop04:02
4.6Iterators05:03
4.7While Loops03:35
5.Using JavaScript13 lessons, 1:44:36
5.1Working With Strings09:32
5.2Template Literals05:46
5.3Working With Numbers06:57
5.4Working With Arrays12:53
5.5Iterating and Transforming Arrays07:33
5.6Working With the Object Type13:55
5.7Object Literal Extensions06:45
5.8Working With Object Instances06:45
5.9Getters and Setters05:00
5.10Custom Objects11:28
5.11The `Math` API04:54
5.12Working With Dates and Times08:10
5.13The `Array` Constructor04:58
6.Functions8 lessons, 56:07
6.1The `this` Object06:15
6.2Working With Functions10:11
6.3Scope07:37
6.4Arrow Functions06:59
6.5Generator Functions08:13
6.6Closures05:00
6.7Prototypes06:26
6.8Default and Rest Parameters05:26
7.Miscellaneous6 lessons, 52:39
7.1Destructuring Assignments08:09
7.2AJAX08:30
7.3Regular Expressions10:51
7.4More About Regular Expressions08:38
7.5Classes06:48
7.6ES Modules09:43
8.Working With the DOM6 lessons, 37:39
8.1Selecting HTML Elements05:02
8.2Manipulating HTML Elements07:40
8.3DOM Traversal05:25
8.4Adding and Removing Elements04:45
8.5Creating Elements and Other Nodes04:39
8.6DOM Events10:08
9.Web APIs4 lessons, 17:41
9.1The Selector API03:03
9.2Geolocation05:29
9.3Web Storage05:24
9.4Web Workers03:45
10.Asynchronous JavaScript5 lessons, 26:23
10.1Promises09:52
10.2Promise Chaining05:11
10.3The async Keyword03:21
10.4The await Keyword04:04
10.5More About async and await03:55
11.Conclusion1 lesson, 00:43
11.1Conclusion00:43
4.1 Conditionals
Hi folks, in this lesson we're going to start looking at how we can work with JavaScript to control the execution flow of our programs. In this lesson we'll focus on using the conditional if statement. Other lessons in this section will cover additional control structures. The if statement is known as a conditional statement, because it only executes certain statements based on the results of an expression. First of all let's look at how it is structured syntactically. In its most Its basic form, the statement begins with the reserved word, if followed by parentheses that contain the conditional expression. Followed by a block, which is denoted by opening and closing curly brackets. So if we run this in a browser now, we should see the console.log in the browser's console, because 1 + 1 is equal to 2. If we go back and change the condition, This time we shouldn't see the log statement in the browser. And we can see that we don't. So the condition to evaluate is within parentheses. Generally, we don't put entire expressions into the parenthesis. We would normally compare a single value, like a variable. Each if, or the corresponding else is known as a branch. And the statements within the curly braces are known as a block. We can add multiple branches by combining the if statement with an else statement. So we shouldn't see the first log, because true is not equal to false, but we will see the second. In this example, we can only really have two branches. But if we’re comparing different kinds of values, we can have as many else branches as we need. So we start out by comparing the color variable with the string blue. And if that expression resolves to true, we'll note this string blue to the console. We then combine the else and if keywords. This time we compare the color variable to the string green. And if that is true, we log the string greens to the console. We then have a final else branch, which basically means that if the first two conditions are not met, then do this branch instead. And I would just log the color variable to the console. So in this case we should see the string red in the console. So we can add as many else-if branches as we need to. But we should really only have one else, even if we have multiple else-if expression. It's not essential that we provide a plain else at the end of the if statement. But we probably will in many cases. One point to note is that any truthy value will cause a branch to execute, not just the boolean value of true. So in this case, the value one is a truthy value. And what truthy means is that in a conditional expression such as an if statement, the value resolves to true, even though it isn't actually the boolean true. This can be very useful. For example, if we want to check that an array contains at least one item before we try to use that array, we can check that it has a length property. So here we define an array that has a single value, which is the string yes. The value that the array contains is not important. It's just important that it contains a value. So when an array contains values, it has a positive value for it's length property. So in this case the length would be one because the array contains 1 value. 1 is a truthy value, so we should see the console log array has length. If change the arrays, so that it doesn't contain any items. The reason why we don't see the console log Is because the length of the array is now zero and zero is falsie not truthy. There is one more type of conditional in addition to the if statement, and that is the ternery statement. This type of condition though, is much more tallest than a standard if else statement. But it's basically, a short cut for a conditional that contains a single if, and a single else. The ternary operator is a special operator that takes three operands. Let's look at the syntax. So the different operands in a turnery are as follows. We have a conditional. That is usually surrounded by parentheses. Although these parenthesis are not mandatory, it does improve readability if you include them. We then use the ternary operator, which is the question mark symbol. This is followed by the truthy operand, in this case a console.log statement. And lastly we have the falsie operand. And that is separated from the truthy operand using a colon. So in this case, because true evaluates to true in the conditional parts of the turnery right at the start of the expression, we should see the first console log. But if we change the conditional so that it evaluates to false, or a false value by changing it to the value false in this example in this case we should see the third operand the falsie operand. So in this lesson we looked at the conditional control structure if along with its else and if else permutations. We learned that any truthy expression will cause the if block to execute. But falsey values will not cause the if block to execute. We also looked at a ternary operator, which is like a shorthand version of a standard two-branch if else statement. In the next lesson, we're going to look at the switch statement. Thanks for watching.