A function is a block of code designed to perform a specific task. Functions are used to organize code, make it more modular, and facilitate code reuse.
function function_name(parameter1, parameter2,...) {
// Your code
return value;
}
// Calling the function
function_name(arguments...);
// Define a function named "hello"
function hello(name) {
console.log("Welcome, " + name + "!");
}
// Call the function
hello("Coders"); // Output: Welcome, Coders!
Explanation:
It is another way to define a function.
let hello = function(name) {
console.log("Welcome, " + name + "!");
}
let str = hello("Coders");
console.log(str); // Output: Welcome, Coders!
A JavaScript function is executed when it is invoked.