A BLOCK OF CODE ==> PERFORMS A TASK
There are multiple ways of function declaration.
function square(number) {
// Function declaration
return number * number;
}
// FUNCTION CALL (calling / executing a function)
const ans = square(6);
console.log(ans); // 36
The advantage of using a function declaration is having an access to 'this' keyword.
Another way of declaring a function.
// const varName = function(params){
//statements
// }
const sayHi = function (name) {
console.log(`Hello, ${name}`);
};
sayHi("Shubham");
A modern way of writing functions in JavaScript.
// const varName = (params) => {
// statements
// };
const sayHi = (name) => {
console.log(`Hello, ${name}`);
};
sayHi("Shubham");
const square = (number) => {
return number * number;
};
// A short hand to declare arrow function if there is just one statement in the function:
const square1 = (number) => number * number;
Parameters are used when defining a function
Arguments are passed when making a function call.
const sayHi = (name, age = 0) => {
// By default we can make the
// value of age to be 0
// to avoid undefined as a
// result when age is not being passed.
// name and age are parameters here
console.log(`Hi ${name}, you are ${age} years old!`); // Hi Shubham, you are 0 years old.
};
sayHi("Shubham"); // 'Shubham' is an argument that is being passed
There are three types of Scope :
- Global Scope
- Function Scope
- Block Scope
the global scope is the scope that contains, and is visible in, all other scopes.
const firstName = "Shubham";
const GlobalScope = () => {
console.log(firstName); // Shubham
};
GlobalScope();
Local variables have Function Scope. They can only be accessed from within the function.
const someFunction = () => {
let firstName = "John";
console.log(firstName); // John
const someFunction2 = () => {
console.log(firstName); // John
};
someFunction2();
};
someFunction();
This scope restricts the variable that is declared inside a specific block, from access by the outside of the block.
if (true) {
var firstName = "Jane";
}
console.log(firstName);
// Declaring a variable with var
// inside a blockscope can also let
// you access the variable in global scope.