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

And you call the function:

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

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.

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.

This is how you define a function using an expression:

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:


Read More

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s