3 Type Function in Javascript

mahin678

Mahin Tazuar

Posted on September 20, 2021

3 Type Function in Javascript

Hello there, maybe you feel weird but today we are talking about 3 types of functions. After 2015 javascript have massive change. Bring something syntactical sugar and also more. So let's start exploring more tricky things.

We have divided the javascript function into 3 types for better understanding.

  1. Normal Function: That is a very common function and we are using that every day. That has a simple syntax and is very easy to use that. we can also pass data as a argument. example :
function showme(x){
console.log('hello world' + x) }
showme('your name');
Enter fullscreen mode Exit fullscreen mode
  1. Function expression: normal function and function expression do have not much difference between them. just have some syntax differences. After defining the function we need to call the function with any argument or without argument under a variable. That has so many advantages. we can use this function where we need it. That is very simple from normal function.
const f = function get (){
  return 'hello';
}
console.log(f)
Enter fullscreen mode Exit fullscreen mode
  1. Arrow function: maximum newbie programmer very confused about arrow function. The first benefit in the arrow function, that is helps to keep code more clean, clear, and understandable. If only one statement has in the function we can return that only one line code. we do not use function keywords and also return keywords. If we need to use more statements we can use that inside the second breaker. But that is not the only main thing. The main thing in the arrow function is the 'this' keyword.that is called some times Lexical 'this' keyword. Arrow function did not do something or do not access in 'this' keyword value. That's why we can use that as a call-back function in many methods.
normal function
const dt = {
    user: 'mahin',
    age:28,
    location:['mymensingh', 'Dhaka'],
    doAction:function(){
     this.location.forEach(function(info){
        console.log(this.user,info); // its provide error (undefined)
     })
    }
}
dt.doAction() 

// its working on another code editor

arrow function 
const dt = {
    user: 'mahin',
    age:28,
    location:['mymensingh', 'Dhaka'],
    doAction:function(){
     this.location.forEach((info)=>console.log(this.user,info))
    }
}
dt.doAction() // its working on other code editor
Enter fullscreen mode Exit fullscreen mode

support: sumit saha , javascript with junaied

💖 💪 🙅 🚩
mahin678
Mahin Tazuar

Posted on September 20, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related