JS Functions
In this page:
function functionName(parameter1, parameter2) {
// body
return value;
}
functionName(argument1, argument2);
Function Declaration
A function declaration defines a named, reusable block of code using the function keyword, which can then be called by that name anywhere it's in scope.
Example: Function Declaration
// Define the function `greetUser` with no parameters
// Define the function `greetUser` with no parameters
function greetUser() {
// Print "Hello!" to the console
// Print "Hello!" to the console
console.log("Hello!");
}
// Call `greetUser()`
// Call `greetUser()`
greetUser();
Parameters and Arguments
Parameters are the named placeholders listed in a function's definition, while arguments are the actual values supplied when the function is called, parameters describe what a function expects, arguments provide it.
Example: Parameters and Arguments
function add(a, b) {
console.log(a + b);
}
add(2, 3); // 2 and 3 are arguments
Return Values
The return keyword sends a value back out of a function to whatever called it, and as soon as return runs, the function stops executing immediately, skipping any code after it.
Example: Return Values
// Define the function `square` taking `n`
// Define the function `square` taking `n`
function square(n) {
// Return `n * n` from this function
// Return `n * n` from this function
return n * n;
}
// Print `square(4)` to the console
// Print `square(4)` to the console
console.log(square(4));
Default Parameters
Default parameters let you specify a fallback value directly in the function definition, which is used automatically whenever the caller doesn't provide an argument for that parameter.
Example: Default Parameters
// Define the function `greet` taking `name`
// Define the function `greet` taking `name`
function greet(name = "Guest") {
// Print `Hello, ${name}!` to the console
// Print `Hello, ${name}!` to the console
console.log(`Hello, ${name}!`);
}
// Call `greet()`
// Call `greet()`
greet();
// Call `greet("Amit")`
// Call `greet("Amit")`
greet("Amit");
Function Expressions and Hoisting
A function expression stores a function inside a variable rather than giving it a standalone name, and unlike function declarations, which are hoisted and callable anywhere in their scope, function expressions are only usable after the line where they're defined.
Example: Function Expressions and Hoisting
// Declare the constant `sayHi`, set to `function () {`
// Declare the constant `sayHi`, set to `function () {`
const sayHi = function () {
// Print "Hi!" to the console
// Print "Hi!" to the console
console.log("Hi!");
};
// Call `sayHi()`
// Call `sayHi()`
sayHi();
- Forgetting the return keyword, a function without an explicit return always produces undefined, even if it looks like it computes something.
- Confusing a function's parameters, the placeholders in its definition, with its arguments, the actual values passed in when it's called.
- Calling a function expression before its declaration line, unlike function declarations, expressions are not hoisted the same way.
- A function declaration defines a reusable, named block of code that can accept parameters and return a value.
- Default parameters let a function fall back to a preset value when an argument isn't provided.
- Function declarations are hoisted and usable before their definition line, function expressions are not.
Function declarations, expressions, and default parameters are all standard JavaScript features supported in every modern browser.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: