Data Types Exercise 4
Falsy values are values that evaluate to false in a boolean context. The falsy values in JavaScript are: false, 0, "" (empty string), null, undefined, NaN, and 0n (BigInt zero).
Truthy values are values that evaluate to true in a boolean context. Any value that is not falsy is truthy. Examples include non-zero numbers, non-empty strings, objects, and arrays.
You can convert a number to a string using String(num), num.toString(), or template literals: `${num}`. Example: String(42) returns "42".
Use String(bool) or bool.toString(). Example: String(true) returns "true" and String(false) returns "false".
Number() converts a value to a number. It returns 0 for false and empty strings, 1 for true, NaN for non-numeric strings, and 0 for null. Example: Number("42") returns 42.
String() converts a value to its string representation. Example: String(123) returns "123", String(true) returns "true", and String(null) returns "null".
Boolean() converts a value to a boolean. It returns false for all falsy values and true for all truthy values. Example: Boolean(0) returns false, and Boolean("hello") returns true.
Use the Array.isArray() method. Example: Array.isArray([1, 2, 3]) returns true. Avoid using typeof for this check, as it returns "object" for arrays.
An array is an ordered collection of values accessed by numeric indexes, while an object is an unordered collection of key-value pairs accessed by named keys. Both are reference types, but arrays have a length property and array-specific methods.
A reference type is a data type whose value is stored as a reference (memory address) rather than directly. Objects, arrays, and functions are reference types. When you assign a reference type to another variable, both variables point to the same object in memory.