What are JavaScript Functions?

A function takes some input and returns an output where there is some relationship between the input and the output.

To use a function, it must be defined somewhere in the scope from which it can be called.


Also called as function definition or function statement.

It consists of the function keyword followed by:

The name of the function List of parameters to the function JavaScript statements enclosed in curly brackets, {…}.

* * *

function square(number)

return number * number;


Primitive parameters are passed to functions by value.

If the function changes the value of the parameter, this change is not reflected globally or in the calling function.

Non-primitive parameters are passed by reference.

When the function changes its properties, that change is visible outside the function.


Functions can also be created by a function expression.

Such a function can be anonymous or it can have a name.

Function expressions are convenient when passing a function as an argument to another function.


* * *

/ / The function receives a function defined by a function expression and executes it for every element of the array received as a second argument.

function map(f, a) {

let result = []; // Create a new Array

let i; // Declare variable

for (i = 0; i  ≠  a.length; i++)

result [i] =f(a[il]);

return result;


const f = function(x) {

return X * X * X;


let numbers = [0, 1, 2, 5, 10];

let cube = map(f, numbers);

console.log(cube) ;

/ / Function returns: [0, 1, 8, 125, 1000] .


Defining a function does not execute it.

Defining it names the function and specifies what to do when the function is called.

Calling the function actually performs the specified actions with the indicated parameters.


Functions must be in scope when they are called, but the function declaration can be hoisted.

The scope of a function is the function in which it is declared (or the entire program, if it is declared at the top level).

Note: Function hoisting only works with function declarations-not with function expressions.

* * *


Leave a Reply

Your email address will not be published. Required fields are marked *