What Is a Return Statement?
π§ͺ Concept:
A return statement sends a value back from a function. Instead of just doing something, the function gives you something back β like a vending machine for code.
function add(a, b) {
return a + b;
}
let sum = add(3, 4);
console.log(sum); // 7
Here, the function adds the numbers and returns the result. You can store that result, use it in other code, or pass it to something else.
π§° Why It Matters:
Returning values lets your functions do more than just print things β they can calculate, combine, transform, and share results with the rest of your program.
π Quick Challenge:
Write a function that returns your favorite emoji:
function getEmoji() {
return "π";
}
console.log(getEmoji()); // π
π§ Fun Analogy:
A return statement is like a reply in a conversation. You ask a function for something β and it talks back.
π Further Reading:
βͺ MDN Docs β return
βͺ W3Schools β JavaScript Return Statement
Subscribe to my feed to follow along. Iβll keep the lessons short and sweet.