Loading lessons...
Function Parameters and Return
Function Parameters and Return
Functions can take inputs (parameters) and send back an output (return value).
Parameters
function greet(name) {
console.log("Hello, " + name);
}
greet("Ada"); // Hello, Ada
The parameter name is a variable the function receives. When you call with an argument, the value flows in.
Multiple parameters
function add(a, b) {
return a + b;
}
Return values
return sends a value back to whoever called the function:
function add(a, b) {
return a + b;
}
let sum = add(3, 5); // sum is 8
The return statement
- Stops the function and sends the value back.
- A function without a return statement returns
undefined.
Default parameters
function greet(name = "World") {
console.log("Hello, " + name);
}
greet(); // Hello, World
greet("Ada"); // Hello, Ada
TL;DR
- Parameters are inputs a function receives.
- Arguments are the values you pass when calling.
returnsends a value back.- Functions without return give undefined.
- Default parameters kick in when no value is passed.