Function Expression & IIFE

Function Expression & IIFE

What are JavaScript functions?

Functions are essential building blocks in JavaScript, they are packed with logic for performing tasks. A function can be likened to a procedure that performs a task or calculates a value. However, for this procedure to be considered as a function, it needs to take some input and return an output.

Function Expression

  • A function can be declared using a function expression. It is declared quite differently from the general syntax because it uses a variable to denote the name of the function. This variable comes before the keyword function.

Function Expressions are two types:

  1. Anonymous Function Expression - Anonymous function declaration allows function names to appear hidden in the declaration itself. In the general function declaration syntax, the function name is attached to the function keyword, but an anonymous function is declared using only the function keyword and a parenthesis.
  2. Named Function Expression Using the function keyword followed by a name that can be used as a callback to that function is known as using a named function in JavaScript.

No alt text provided for this image

In the above code a lot of things will happen. Let's talk about that-

In The above code we can see there are a lot of ways to implement a function. In JavaScript everyone has a specific meaning so lets see-

Whenever your function declaration starts with a keyword function(in line no 1 fig) that is called function declaration. If you see in line no. 4(in fig) we are defining a function but the first expression is not a keyword function it is let in that case it is called a function expression . In line no 10 & 14 first valid piece of code is parenthesis here it is not a keyword function so here also it is a function expression.

No alt text provided for this image

Let's Talk About IIFE

IIFE stands for Immediately Invoked Function Expression. It means you wrote a function expression and you immediately call it, that is IIFE.

No alt text provided for this image

Use Case of IIFE

No alt text provided for this image

Use Case of Named Function

The main use Case of the named function is Readability. It increases the readability of the code if you have a very huge code base and you just passed your function expression without the name making it anonymous and do that nobody stops you but the readability of the code decreases.This is the first point which is in favor of named function expression. Another use case, in recursion if we use anonymous function expression and we perform recursion on it then how do you do recursion? That's problematic so for recursive cases the named function is helpful.

No alt text provided for this image

要查看或添加评论,请登录

Shubhasheesh Kundu的更多文章

  • Promises

    Promises

    Promises are to improve code readability. With the help of promises we can solve the problem of Inversion of control.

  • Async Programming With JavaScript

    Async Programming With JavaScript

    Properties of JavaScript JavaScript is sync in nature. JavaScript is single threaded.

  • Higher Order Function, Callbacks Function, Biggest Problem With Callbacks

    Higher Order Function, Callbacks Function, Biggest Problem With Callbacks

    Higher Order Function There are functions which take another function as arguments, these are called high order…

  • JavaScript - Boxing Concept

    JavaScript - Boxing Concept

    Primitives- Boxing Concept In JavaScript and other languages, primitive values don’t have methods or properties, so if…

  • JavaScript - 3

    JavaScript - 3

    ToBoolean ( argument ) - The abstract operation ToBoolean converts argument to a value of type Boolean according to…

  • JavaScript - 2

    JavaScript - 2

    Let’s Talk About [Object Object] You might have seen this kind of [object object] string getting printed on web apps or…

  • JavaScript

    JavaScript

    You might find lot of memes or you might find lot of articles were people say that JavaScript is weird Programming…

    1 条评论

社区洞察

其他会员也浏览了