With the use of certain operators, true is treated like 1 and false like 0. In your console, you can verify this.
Truthy and falsy are what we use to determine the result of a conditional expression. They are what we use to determine its result, they are not the result.
- The empty string ‘’
- The number 0
- The number NaN (yep, ‘Not a Number’ is a number, it is a special number)
Everything else is truthy, and that includes
Infinity (which is another special number, like NaN), and all Object objects and Array objects, empty or not.
Let us first discuss double equals “==” aka loose equality, in loose equality, first it converts into command type, and then it compares equality.
Triple equal “===” aka strictly equality, in strict equality, it does not perform any data type conversion, if both values are not the same type, it returns a false value.
In both examples, double equal “==”, it first converts the string ‘10’ and number 10 in the same common type and then it compares, so it returns True. But in triple equal “===”, it founds both values are different type and it simply returns False
Understanding the difference between
Undefined is a variable that has been declared but not assigned a value.
Null as an assignment value. So you can assign the value
null to any variable which basically means it’s blank.
undefined. However, when you assign
null to a variable, you are declaring that this value is explicitly empty.
null. That must be done by you in your code.
Let’s get some more info on these.
Typeof Null and Undefined
We see here that the type of
null is an object but the type of
undefined is undefined.
Comparison Using Abstract and Strict Equality
Since these are different data types, if we compare them with strict equality
===, we get
But if we compare them with abstract equality
==, we get
So if you need to check if a value is either
undefined, you can check for abstract equality and compare it to either null or undefined. Both will return the same result.
Those are 3 really powerful array functions:
mapreturns an array with the same length,
filteras the name implies, it returns an array with fewer items than the original array
reducereturns a single value (or object)
findreturns the first items in an array that satisfies a condition
reduce were introduced in ES5, so you can safely use them as implemented in every browser for years.
find was introduced in ES6/ES2015.
They offer a more declarative approach, rather than an imperative approach (describe what should happen, not write every tiny bit of processing that should happen)
Execute something on every element with
A loop would look like this:
This generates a new array, without editing the original one (what we call immutability)
Since we use a single function in the map callback function, we can rewrite the sample as:
Finding a single element in the array
Sometimes you need to look for a specific item in the array and return it.
This is how you would do so with a loop:
Here is the non-loop version, using
Here is the same functionality using
For learning purposes (does not make much sense in practice), here is the same functionality using
reduce() will iterate over all the array items, while
find() will be faster.
Iterate over an array to count the property of each item
reduce() to get a single value out of an array. For example sum the items
using a loop:
can be written as