In JavaScript, a truthy value
is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy.
Examples
if (true)
if ({})
if ([])
if (42)
if ('foo')
if (new Date())
... and everything else (unless defined as falsy)
A falsy value
is a value that translates to false when evaluated in a Boolean context.
Seven cases
if (false)
if (null)
if (undefined)
if (0)
if (NaN)
if ('')
if ("")
Functions can be combined to form new functions through function composition. The function addThenSquare
is made by combining the functions add
and square
.
const add = function(x, y) {
return x + y;
};
const square = function(x) {
return x * x;
};
const addThenSquare = function(x, y) {
return square(add(x, y));
};
Every JavaScript object has a prototype which is also an object. All JavaScript objects inherit their properties and methods from their prototype.
function Something() {} // note capital letter and fact that function is an object in JS
Something.prototype.bar = true; // this could also be a method/function
let something = new Something();
something.bar; // true
something instanceof Something; // true