Skip to content

Latest commit

 

History

History
91 lines (58 loc) · 1.84 KB

javascript.md

File metadata and controls

91 lines (58 loc) · 1.84 KB

JavaScript (ES5 or lower)

Truthy & Falsy

Truthy

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)

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 ("")

Function composition

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));
};

Prototype

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