Tutorial by Examples

A normal function declaration looks like this: function foo(){ } A function defined like this is accessible from anywhere within its context by its name. But sometimes it can be useful to treat function references like object references. For example, you can assign an object to a variable based...
Defining an Anonymous Function When a function is defined, you often give it a name and then invoke it using that name, like so: foo(); function foo(){ // ... } When you define a function this way, the Javascript runtime stores your function in memory and then creates a reference to th...
Sometimes you don't want to have your function accessible/stored as a variable. You can create an Immediately Invoked Function Expression (IIFE for short). These are essentially self-executing anonymous functions. They have access to the surrounding scope, but the function itself and any internal va...
When you define a function, it creates a scope. Everything defined within the function is not accessible by code outside the function. Only code within this scope can see the entities defined inside the scope. function foo() { var a = 'hello'; console.log(a); // => 'hello' } consol...
5.1 When you take a reference to a method (a property which is a function) in JavaScript, it usually doesn't remember the object it was originally attached to. If the method needs to refer to that object as this it won't be able to, and calling it will probably cause a crash. You can use the .bind...
Functions can take inputs in form of variables that can be used and assigned inside their own scope. The following function takes two numeric values and returns their sum: function addition (argument1, argument2){ return argument1 + argument2; } console.log(addition(2, 3)); // -> 5 ...
Functions can either be named or unnamed (anonymous functions): var namedSum = function sum (a, b) { // named return a + b; } var anonSum = function (a, b) { // anonymous return a + b; } namedSum(1, 3); anonSum(1, 3); 4 4 But their names are private to their own scope: ...
A recursive function is simply a function, that would call itself. function factorial (n) { if (n <= 1) { return 1; } return n * factorial(n - 1); } The above function shows a basic example of how to perform a recursive function to return a factorial. Another...
Currying is the transformation of a function of n arity or arguments into a sequence of n functions taking only one argument. Use cases: When the values of some arguments are available before others, you can use currying to decompose a function into a series of functions that complete the work in s...
The return statement can be a useful way to create output for a function. The return statement is especially useful if you do not know in which context the function will be used yet. //An example function that will take a string as input and return //the first character of the string. function...
In JavaScript all arguments are passed by value. When a function assigns a new value to an argument variable, that change will not be visible to the caller: var obj = {a: 2}; function myfunc(arg){ arg = {a: 5}; // Note the assignment is to the parameter variable itself } myfunc(obj); conso...
Functions have two built-in methods that allow the programmer to supply arguments and the this variable differently: call and apply. This is useful, because functions that operate on one object (the object that they are a property of) can be repurposed to operate on another, compatible object. Addi...
Before ECMAScript 2015 (ES6), a parameter's default value could be assigned in the following way: function printMsg(msg) { msg = typeof msg !== 'undefined' ? // if a value was provided msg : // then, use that value in the reassignemnt 'Default value for ...
To create a function which accepts an undetermined number of arguments, there are two methods depending on your environment. 5 Whenever a function is called, it has an Array-like arguments object in its scope, containing all the arguments passed to the function. Indexing into or iterating over th...
6 ES6: myFunction.name Explanation on MDN. As of 2015 works in nodejs and all major browsers except IE. 5 ES5: If you have a reference to the function, you can do: function functionName( func ) { // Match: // - ^ the beginning of the string // - function the w...
Similar to currying, partial application is used to reduce the number of arguments passed to a function. Unlike currying, the number need not go down by one. Example: This function ... function multiplyThenAdd(a, b, c) { return a * b + c; } ... can be used to create another function that...
Composing multiple functions into one is a functional programming common practice; composition makes a pipeline through which our data will transit and get modified simply working on the function-composition (just like snapping pieces of a track together)... you start out with some single responsi...

Page 1 of 1