Function Declarations

Contact Us or call 1-877-932-8228
Function Declarations

Function Declarations

Perhaps the most basic manner in which we can create functions in JavaScript is with a function declaration, often called a function statement. It's the way in which most of us first learn about functions and perhaps the most familiar:

function foo(bar) {
	return bar + 3;
}

Here we defined a function named foo which accepts a parameter bar and returns the value of bar plus three. We can call our foo function later

document.write(foo(9));

to write, in the example above, the value 12 to the screen. Because function declarations are hoisted - moved by the JavaScript interpreter to the top of the executing code and evaluated before execution flow - we could even invoke our function foo before declaring it. (This is true for variable declarations in JavaScript, too. Many developers suggest not relying on this feature but rather, for clarity and code organization, declaring variables and functions before they are used.)

Next