Creating your first function is an exciting step in learning JavaScript. A function lets you group instructions together and run them whenever you want.
Once you understand how to create a function, you can start organizing your code in a cleaner and more professional way instead of writing everything again and again.
Basic Structure of a Function
A function in JavaScript has three main parts:
- The function keyword
- The function name
- The code block inside { }
This structure helps JavaScript understand what task the function should perform.
This code creates a function named sayHello. At this point, the function is only defined — it will not run until you call it.
Here, the function is called using its name followed by parentheses. When the function is called, the code inside it runs.
These examples show how easy it is to create and use functions.
By mastering how to create your first function, students build the foundation for writing organized, reusable, and professional JavaScript programs.