Exercises | | fizz buzz | Search

falsey javascript

Cell 0

In JavaScript, an empty array [] is considered a truthy value, meaning it's treated as true in a boolean context, resulting in unexpected comparisons when using the loose equality operator ==. This leads to [] == true returning true and [] == false returning false.

Cell 1

In the given code breakdown, console.log() prints output to the console, while the OR operator (||) returns the first truthy value from its operands. In JavaScript, empty arrays ([]) are considered falsy values, whereas non-empty arrays (['not falsey enough']) are considered truthy values.

Cell 2

This line of code uses the logical OR operator (||) to evaluate two expressions, an empty string ('') and a non-empty string ('unempty strings'). Due to how JavaScript handles falsy values, it will always log 'unempty strings' to the console, regardless of the value of the first expression.

Cell 3

The JavaScript code console.log(0 || 1) logs the result of the conditional expression 0 || 1 to the console, which evaluates to 1 because 1 is the first "truthy" value encountered. This is due to the || operator's behavior of returning the first truthy value it encounters.

Cell 4

The provided code uses the logical OR operator (||) to return an object with a default property set to true if the left side (a null value) is falsy. In this case, the expression returns { default: true } because null is considered falsy in JavaScript.

Cell 5

The JavaScript code attempts to log the type of an undefined variable undef to the console, but throws a ReferenceError instead. When trying to log the result of the expression undef || {"too much": false}, it returns the object literal {"too much": false} to the console due to the undefined value of undef.

Cell 6