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
Hemos revisado y comprobado los materiales, pero aún pueden existir errores. El contenido se ofrece únicamente con fines educativos, así que úsalo bajo tu propia responsabilidad y verifica con otras fuentes si es necesario.
✨ Pregunta a Lara — tu compañera de estudio con IA
Desbloquea soporte de aprendizaje personalizado. Lara puede explicar lecciones, resumir temas y responder tus preguntas — disponible desde el plan Go y superiores.
Lara te ayuda a aprender más rápido — exclusivo para los miembros ReadyTools Go, Plus y Max.
