- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Functions
JavaScript Functions
JavaScript Functions
A JavaScript
functionis a block of code that you can name once and run whenever you need it.
Instead of writing the same lines again and again, you put them inside a function and call the function by its name.
A function only runs when something calls it.
Example
Example
javascript
function greet() {
console.log("Hello");
}
greet();In this example, the function is named greet.
Nothing is printed until greet() is called on the last line.
What is a Function?
A function is a named block of code that performs one job.
