Truthy and Falsy Values
• if (true)
• if ({})
• if ([])
• if (42)
• if ("0")
• if ("false")
• if (3.14)
• if (-3.14)
• if (Infinity)
• if (-Infinity)
• if (new Date())
• if (-42)
In JavaScript, a truthy value is considered true when encountered
in a Boolean context
Truthy and Falsy Values
if (false) {
// Not reachable
}
if (null) {
// Not reachable
}
if (undefined) {
// Not reachable
}
if (0) {
// Not reachable
} … and so on
In JavaScript, a falsy value is considered false when encountered in a Boolean
context. All values are truthy except false, 0, -0, “ ”, null, undefined, NaN
Arrays
• push(), adds new element at end of array
• pop(), removes last array element
• length, gives the length of array
• concat(), joins two arrays into a new single one
• slice(), returns a new array of elements consisting of the start and end index.
Array Magic
The map() method creates a new array populated with the results of calling a
provided function on every element in the calling array.
Array Magic
The reduce() method executes a user-supplied "reducer" callback function on each element of the
array, in order, passing in the return value from the calculation on the preceding element. The final
result of running the reducer across all elements of the array is a single value.
Factory Functions
One of the biggest issues with constructors is that while they look just like
regular functions, they do not behave like regular functions at all. If you try to
use a constructor function without the new keyword, your program will not
work as expected, but it won’t produce error messages that are easy to
trace.