Control Flow, typeof, and Math
Table of Contents
Slides
Overview
Key Terms
Comparison Operators - Operators that compare two values and return a boolean
Logical Operators - Operators used to combine conditions, such as && (AND), || (OR), and ! (NOT)
Type Coercion - The automatic conversion of a value from one data type to another
Guard Clause - A statement used to check if the remaining lines of code should be executed or not
Scope - Scope is the current context of execution in which values and expressions are "visible" or can be referenced
Control Flow Review
Programs usually change their behavior based on inputs.
Conditions and Booleans help decide if something is
trueorfalse.Use
if/else,switch, or ternary operators to decide what to do in a given situation.They do this with Comparison Operators and Logical Operators which return a Boolean.
if/else statements
if/else statements3 parts:
if,else if,else.Starts checking at the top and stops checking when it hits a match.
Only 1 block is executed, so be specific first and get more general.
Example:
Conditions in an
ifstatement must evaluate totrueorfalse.
Comparison Operators:
Comparison Operators compare two values of the same type.
===
Equal To
'Hi' === 'Hi'
true
!==
Not Equal To
6 !== 7
true
!=
Loose Equal To
"6" == 6
true
!=
Loose Not Equal To
6 != "6"
false
>
Greater Than
10 > 1
true
<
Less Than
30 < 5
false
>=
Greater or Equal
5 >= 5
true
<=
Less or Equal
2 <= 2
true
Logical Operators:
Logical Operators compare the outcomes of two conditions and let you be more expressive
&&
AND
5 > 3 && 2 > 3
false
5 > 3 && 4 > 3
true
||
OR
5 > 3 || 2 > 3
true
!
NOT
!(5 > 3)
false
We can use
()to group together each condition
Coercions and Truthiness
Type Coercion ("co-er-shun") — JavaScript will automatically convert (coerce) types when necessary. For example,
'1' + 1results in'11'.Truthy/Falsy Values — any expression you use for an
ifstatement's(condition)will be coerced into a Boolean."Falsy" values are coerced to be
falseand only these 5 values will behave this way:0,"",null,undefined, andNaNAll other values are truthy
Simplify Your Logic
Guard Clauses and Ternary Operators
Code should be readable.
Avoid deeply nested
if/elsestatements. Use guard clauses and ternary operators.A guard clause is an if statement that returns before subsequent has a chance to be executed.
ifandelsestatements let you choose between one of two code blocks to execute.The ternary operator
condition ? valA : valBis used to choose between one of two values.
Scope and Variables
If you want to get variables out of blocks, you have to define them in a higher scope.
Values in a higher scope are visible to lower scopes.
Example:
typeof Operator
typeof valuereturns the type of a givenvalueas a string. It’s an operator, not a function.Example usages:
Identifying null, Arrays, and NaN
null, Arrays, and NaNUnfortunately,
typeoftells us that arrays are objects (they are) so we have to useArray.isArray()to check if something is an array.It also tells us that
nullis an Object so we have to check that manuallyIt also tells us that
NaNis a number, which it is, but to know if something isNaN, we can use theisNaN()function
Math Operations
Use basic math operators:
+,-,/,*.The power operator
x ** yraises x to the power of y.%returns the remainder after division.
The Math Object
Math ObjectIn JavaScript
Mathis a globally available object with methods and properties for doing more complex math.
Here is an example of a function that makes use of
Math.random()to flip a coin
Last updated