Functions
In JavaScript, functions are reusable blocks of code that perform a specific task. They allow you to organize your code into logical units and avoid repetition. Functions can take input values, called parameters, and return a value.
Function Declaration
You can define a function using the function keyword, followed by the function name and a pair of parentheses. Any parameters the function takes are listed inside the parentheses.
Here's an example of a function that greets the user by name:
JAVASCRIPT
1function greeting(name) {
2 console.log(`Hello, ${name}!`);
3}Function Call
Once a function is defined, you can call it by using the function name with a pair of parentheses. If the function takes parameters, you provide the values inside the parentheses.
Here's an example of calling the greeting function with the parameter "John":
JAVASCRIPT
1// Function Call
2
3greeting("John");Output:
SNIPPET
1Hello, John!xxxxxxxxxx// Function Declarationfunction greeting(name) { console.log(`Hello, ${name}!`);}// Function Callgreeting("John");OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment



