A function is a set of instructions written once and used many times.
function sayHi() {
console.log("Hi!");
}
sayHi(); // Hi!
sayHi(); // Hi! (use it again!)function func_name(params) { statements }
function greet(name) {
console.log("Hi, " + name + "!");
}const func_name = function(params) { statements };
const greet = function(name) {
console.log("Hi, " + name + "!");
};const func_name = (params) => { statements };
const greet = (name) => { console.log("Hi, " + name + "!"); };Tip
functionkeywordfunctionandconstkeywordsconstkeyword
Parameter = the placeholder in the definition of the function
Argument = the real value passed into the function when you call it
// 👇 parameters
function add(num1, num2) {
console.log(num1 + num2);
}
// 👇 arguments
add(10, 5); // 15
add(x, y); // x + yUse return when you want the function to give you a result.
function multiply(a, b) {
return a * b;
}
const answer = multiply(4, 5);
console.log(answer); // 20Important
After return, the function stops. Nothing below it runs.
function check(age) {
if (age >= 18) return "Adult";
return "Minor";
console.log("Thank you!"); // Never executes
}
console.log(check(20)); // Adult
console.log(check(15)); // MinorDefault parameters are set to maintain the function flow incase of no arguemnets being passed in the function call.
function welcome(name = "Guest") { // Take value of name as "Guest" whenever no arguements in function call
console.log("Welcome, " + name + "!");
}
welcome("Priya"); // Welcome, Priya!
// When no arguements are passed
welcome(); // Welcome, Guest!Scope of a variable is the region of the code where it is accessible.
When variables are declared inside a function, they are called as local variables (function variables) and cannot be accessed outside of the function.
function myFunc() {
const secret = "I'm inside!";
console.log(secret); // Works
}
myFunc();
console.log(secret); // secret doesn't exist hereFunction callback is used when you want some set of functions to be executed in order without fail.
const sum = (callback, x, y) => {
let result = 0;
setTimeout(() => { result = x + y: }, 3000); // wait for 3s (3000 ms) and then execute { result = x + y; }
callback(result);
}
const display(result){
console.log(result);
}
// You want the functions to execute in the order sum() --> display() strictly
// Hence send display as a callback into sum() asking it to execute display() once itself is completely executed.
sum(display, 12, 18); // 18
// No need to write display function separately.