JS MASTER CLASS /// RETURN STATEMENT /// PARAMS & ARGUMENTS /// JS MASTER CLASS /// RETURN STATEMENT /// PARAMS & ARGUMENTS /// JS MASTER CLASS /// RETURN STATEMENT /// PARAMS & ARGUMENTS ///

JS Return & Params

Learn how to feed data into your functions using parameters and extract results using the powerful return keyword.

JS functions.js
1 / 12
12345
⚙️🥤

Tutor:Think of a JavaScript function like a vending machine. You put something in, the machine processes it, and it gives something back.


Skill Matrix

UNLOCK NODES BY MASTERING JS CONCEPTS.

Concept: Parameters

Parameters are variables that act as placeholders for the values that are to be input to a function when it is executed.

System Check

Where do you define parameters?


Community Holo-Net

Share Your Logic

ACTIVE

Wrote a cool utility function? Share your parameters and returns with fellow developers.

JS Functions & Returns

Author

Pascual Vila

Fullstack Engineer // Code Syllabus

Functions in JavaScript are block of code designed to perform a particular task. But to make them truly useful and dynamic, they need to take inputs (Arguments & Parameters) and give an output (Return).

Parameters vs Arguments

Often used interchangeably, but there's a distinct difference. Parameters are the variable names listed in the function definition. Arguments are the real, actual values passed to the function when you execute it.

// 'name' and 'age' are parameters
function greet(name, age) {
  console.log("Hi " + name + ", age: " + age);
}

// "Alice" and 25 are arguments
greet("Alice", 25);

The Return Keyword

When JavaScript reaches a return statement, the function will stop executing immediately. The value following the return keyword is passed back to whoever called the function.

If you don't explicitly return a value, the function will return undefined. Printing a value using console.log() inside a function is not the same as returning it!

Default Parameters (ES6)

You can assign default values to parameters. If no argument is passed during the call, the function defaults to that pre-assigned value instead of undefined.

function sayHello(name = "Stranger") {
  return "Hello, " + name;
}
console.log(sayHello()); // "Hello, Stranger"
Advanced Topic: Early Returns+

An "early return" is a pattern where you return from a function as soon as a condition is met, rather than using deep nested `if-else` blocks.

By using `return` early, you prevent the rest of the code in the function from running, acting as a natural break point and making your code significantly cleaner and easier to read.

JS Functions Glossary

Function

A reusable block of code designed to perform a particular task.

snippet.js

Parameter

A named variable passed into a function definition. It acts as a local variable inside the function.

snippet.js

Argument

The actual value passed into the function when it is invoked/called.

snippet.js

Return

A statement that stops function execution and sends a value back to the calling code.

snippet.js

Undefined

The default return value of a function if no return statement is specified.

snippet.js

Default Parameter

A value assigned to a parameter in case no argument is provided during the function call.

snippet.js