Function:
A Function is a block of code written to perform a specific task.
Instead of writing the same code multiple times, we can place it inside a Function and call it whenever needed.
1. Parameters (In Function Definition):
Parameters are the variables written when creating a function
function biriyani(rice, masala, chicken) {
console.log(rice, masala, chicken);
}
Here:
rice, masala, chicken
These are called Parameters
2. Arguments (In Function Call):
Arguments are the actual values passed when calling a function
biriyani("Basmati Rice", "Spicy Masala", "Chicken");
Here:
"Basmati Rice"
"Spicy Masala"
"Chicken"
These are called Arguments
function eat(food) { // food = Parameter
console.log(food);
}
eat("Biriyani"); // "Biriyani" = Argument
Parameter = In the function definition
Argument = In the function call
Return:
-
returnis used to send a value out from a function orreturnsends a value back from a function
function biriyani(riceContainer, container, masalaContainer)
{
console.log(riceContainer, container, masalaContainer);
console.log("biriyani ready");
return "ChickenBiriyani";
}
function eat(food)
{
console.log(food);
}
biriyani('rice', 'masala', 'chicken');
eat();
Output:
1. Function With Arguments:
biriyani('rice', 'masala', 'chicken');
riceContainer, container, masalaContainer→ Parameters'rice', 'masala', 'chicken'→ Arguments
The biriyani() function receives values when it is called, so it is a Function With Arguments
2. Function With Return Value:
return "ChickenBiriyani";
The biriyani() function returns a value ("ChickenBiriyani"), so it is a Function With Return Value
3. Function Without Arguments:
eat();
The eat() function is called without passing any value, so it is a Function Without Arguments
However:
function eat(food) {
console.log(food);
}
This function has a parameter (food), but you called it without an argument. Therefore food becomes undefined
Hoisting in JavaScript:
Hoisting means JavaScript moves declarations to the top of their scope before the code runs
console.log(name);
var name = "Raja";
Output:
JavaScript treats it like:
var name;
console.log(name);
name = "Raja";
Output:
Function Hoisting:
sayHello();
function sayHello() {
console.log("Hello");
}
Output:
Because function declarations are hoisted completely
let and const:
console.log(age);
let age = 25;
Output:







Top comments (0)