Functions in JavaScript
Functions are one of the most powerful tools in JavaScript. They let you group code that does a specific task and reuse it whenever you need – like a recipe you can follow again and again.
Instead of writing the same code multiple times, you put it in a function and just 'call' the function when you want it to run. This makes your code cleaner, easier to fix, and much more organized.
How to Create a Function
Use the keyword function, give it a name, add parentheses () for parameters (we’ll talk about those soon), and curly braces {} for the code inside.
The code inside the function only runs when you call it with sayHello(); – perfect for doing something only when needed.
Parameters – Giving Information to Functions
Functions can accept inputs called parameters. Think of them as ingredients for your recipe.
You can have multiple parameters separated by commas.
Return Statement – Getting Results Back
Functions can give back a value with return. This is useful when you want to use the result somewhere else.
Once return runs, the function stops – nothing after it executes.
Why Functions Are So Useful
- Reuse code without copying and pasting.
- Make code easier to read and understand.
- Fix bugs in one place instead of many.
- Organize big programs into small, manageable pieces.
Quick Quiz
We have reviewed and checked the materials, but errors may still occur. The content is provided for educational purposes only, so use it at your own responsibility and verify with other sources if needed.
✨ Ask Lara — your AI study partner
Unlock personalized learning support. Lara can explain lessons, summarize topics, and answer your study questions — available from the Go plan and above.
Lara helps you learn faster — exclusive to ReadyTools Go, Plus, and Max members.

