filmov
tv
5 Different Ways of Writing Functions in JavaScript

Показать описание
5 Ways to Define Functions In JavaScript : JavaScript Functions
There are many ways to write functions in JavaScript, after all, it is javascript, what else do you expect? Anyway, I will show 5 Different ways of writing functions in JavaScript in just 2 mins. I will cover everything including basic functions, arrow functions, even Immediately invoked functions, and many more, so stick around.
So what the hell is a function?
A function is a block of code that is used to perform a specific task and Generally, Functions are executed when you call it or invoke it. Functions make your code reusable.
1. Simple Function: Basic Function declaration In JS
To define a function in JS you need to use the "function" keyword, let's see a basic example :
2. Function with Parameters: Basic Function with parameters
So we will just modify the simple function that we wrote earlier, I will just add the parameters and call the function. Here a and b are parameters.
3. Function Expressions
In js, you can also define a function as an expression and assign it to a variable. And there are two types of Function Expressions. Anonymous Function Expression, and Named Function Expression.
3.1First, Anonymous Function Expression:
Again let's modify the function and turn it into a function expression. Let's assign the function to a variable. Since this type of function has no name, we call it an anonymous function.
3.2.Named Function Expression:
Now for the named function expression, It is quite self-explanatory, Instead of assigning an anonymous function to a variable, we will assign a traditional function to a variable. Well, It is quite useless since you cannot call the function by its name outside its scope.
4. Arrow Function
Now for the cool kids' stuff, Arrow functions were introduced in ES6. Let's turn a traditional function into an arrow function.
You don't have to use the function keyword, Instead, you need to assign it to a variable and add an arrow, you can also remove the curly brackets,and put the function in a single line, like so If the code is not long. Let's add some parameters too.
5. Immediately Invoked Function Expressions :
Immediately Invoked Function Expressions also known as IIFE in JavaScript are a special type of function. This type of function is executed as soon as it is defined. Unlike other functions in Js, you need to enclose the whole function inside () / parentheses. Now let's combine the arrow function with IIFE, shall we? The function might look weird and confusing, but let's try it anyway.
Now we also have Constructors, callbacks functions, and whatnot, But those are for a later video.
There are many ways to write functions in JavaScript, after all, it is javascript, what else do you expect? Anyway, I will show 5 Different ways of writing functions in JavaScript in just 2 mins. I will cover everything including basic functions, arrow functions, even Immediately invoked functions, and many more, so stick around.
So what the hell is a function?
A function is a block of code that is used to perform a specific task and Generally, Functions are executed when you call it or invoke it. Functions make your code reusable.
1. Simple Function: Basic Function declaration In JS
To define a function in JS you need to use the "function" keyword, let's see a basic example :
2. Function with Parameters: Basic Function with parameters
So we will just modify the simple function that we wrote earlier, I will just add the parameters and call the function. Here a and b are parameters.
3. Function Expressions
In js, you can also define a function as an expression and assign it to a variable. And there are two types of Function Expressions. Anonymous Function Expression, and Named Function Expression.
3.1First, Anonymous Function Expression:
Again let's modify the function and turn it into a function expression. Let's assign the function to a variable. Since this type of function has no name, we call it an anonymous function.
3.2.Named Function Expression:
Now for the named function expression, It is quite self-explanatory, Instead of assigning an anonymous function to a variable, we will assign a traditional function to a variable. Well, It is quite useless since you cannot call the function by its name outside its scope.
4. Arrow Function
Now for the cool kids' stuff, Arrow functions were introduced in ES6. Let's turn a traditional function into an arrow function.
You don't have to use the function keyword, Instead, you need to assign it to a variable and add an arrow, you can also remove the curly brackets,and put the function in a single line, like so If the code is not long. Let's add some parameters too.
5. Immediately Invoked Function Expressions :
Immediately Invoked Function Expressions also known as IIFE in JavaScript are a special type of function. This type of function is executed as soon as it is defined. Unlike other functions in Js, you need to enclose the whole function inside () / parentheses. Now let's combine the arrow function with IIFE, shall we? The function might look weird and confusing, but let's try it anyway.
Now we also have Constructors, callbacks functions, and whatnot, But those are for a later video.