Javascript – functions

A function is a group of code that can be executed by calling its name or by the variable name which you have stored a function that has been defined using an expression.

function name(parameters) {
  // block of code
}
, <span>Javascript – functions</span>, KandZ

And you call the function:

, <span>Javascript – functions</span>, KandZ

You can pass parameters to a function by adding the variables inside the parentheses

, <span>Javascript – functions</span>, KandZ

If you do not give a value to the parameter then the value will be set to Undefined. Function in Javascript always return a value. If the function doesn’t have a return value specified by you, it will return undefined.

, <span>Javascript – functions</span>, KandZ
, <span>Javascript – functions</span>, KandZ

If you want to have a function with variable number of parameters, then you don’t have to do anything. When a new function is declared, a new array called arguments is created.

, <span>Javascript – functions</span>, KandZ
, <span>Javascript – functions</span>, KandZ

This is how you define a function using an expression:

, <span>Javascript – functions</span>, KandZ

The above function is an anonymous function. Those functions are called first-class objects and can be manipulated like all the other objects.

Javascript also supports callback functions, which are functions that are passed to another function and they will be executed inside the function you will call. An example:

, <span>Javascript – functions</span>, KandZ
, <span>Javascript – functions</span>, KandZ

THE END!!!

101920cookie-checkJavascript – functionsJavascript – functions

Related posts