Control Flow, typeof, and Math

Follow along with code examples here!

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 true or false.

  • 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

  • 3 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 if statement must evaluate to true or false.

Comparison Operators:

  • Comparison Operators compare two values of the same type.

Operator
Name
Example
Result

===

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

Operator
Name
Example
Result

&&

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' + 1 results in '11'.

  • Truthy/Falsy Values — any expression you use for an if statement's (condition) will be coerced into a Boolean.

    • "Falsy" values are coerced to be false and only these 5 values will behave this way: 0, "", null, undefined, and NaN

    • All other values are truthy

Simplify Your Logic

Guard Clauses and Ternary Operators

  • Code should be readable.

  • Avoid deeply nested if/else statements. Use guard clauses and ternary operators.

  • A guard clause is an if statement that returns before subsequent has a chance to be executed.

  • if and else statements let you choose between one of two code blocks to execute.

  • The ternary operator condition ? valA : valB is 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 value returns the type of a given value as a string. It’s an operator, not a function.

  • Example usages:

Identifying null, Arrays, and NaN

  • Unfortunately, typeof tells us that arrays are objects (they are) so we have to use Array.isArray() to check if something is an array.

  • It also tells us that null is an Object so we have to check that manually

  • It also tells us that NaN is a number, which it is, but to know if something is NaN, we can use the isNaN() function

Math Operations

  • Use basic math operators: +, -, /, *.

  • The power operator x ** y raises x to the power of y.

  • % returns the remainder after division.

The Math Object

  • In JavaScript Math is 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