CC-01: Conditionals

with Carmen Salas β€’ 2024/10/02

slides


Truthys and Falsys

Truthy JS values:

  • '0' - a string containing a single zero

  • 'false' - a string containing 'false'

  • [] - empty array

  • {} - empty object

  • function(){} - an empty function

Falsy JS values:

  • false

  • 0 zero as a number and -0 minus zero as a number

  • '', "" - empty strings

  • null

  • undefined

  • NaN

Important tidbit:

The only way for null and undefined to be true when compared with equality operators is if it is compared to each other (or itself).

typeof Hierarchy in JS:

This is not from any official doc, but something I tested when working with the swe-1-3 homework!

Conditionals Overview

Comparison Operators

The strict equality operator === always considers operands of different types to be different.

  • not all languages implement this operator!

Test this out in node!

Code Challenge

These solutions implement the refactored version, and is NOT the neatest way to write them (sorry!).

  1. Write a function named greatestOfTwo that takes in two integer arguments and returns the largest of both arguments. You can assume both integers will be different values.

  1. Write a function named areBothSame, that takes in two arguments and returns true if both arguments are truthy in value, and returns false if not.

  1. Write a function named areBothEqual, that takes in two arguments and returns true if both arguments are the same in data type and value, and returns false if they are not.

Last updated