Skip to content
the poor coder

iife arrow function

Poor Man
Published
Reading time
1 min read

iife arrow function#

If you are a JavaScript developer, then you might have come across the term IIFE (Immediately Invoked Function Expression) and arrow functions. These are two different concepts, but they can be used together in some cases.

An IIFE is a function that is immediately invoked after it is defined. It is usually used to create a local scope for variables and functions, so they don't interfere with the global scope. Here's an example of an IIFE:

Text

(function() {
  var x = 10;
  console.log(x);
})();

This code creates an anonymous function that is immediately invoked. The variable x is defined inside the function, which means it is not accessible outside of the function.

Now, let's talk about arrow functions. An arrow function is a shorter way to define a function in JavaScript. It was introduced in ES6 (ECMAScript 2015). Here's an example:

Text

const sum = (a, b) => {
  return a + b;
}
console.log(sum(3, 4)); // Output: 7

This code defines an arrow function called sum that takes two parameters (a and b) and returns their sum.

When we combine IIFE and arrow functions, we can create a self-invoking arrow function. Here's an example:

Text

((a, b) => {
  console.log(a + b);
})(3, 4);

This code defines an arrow function that takes two parameters (a and b) and logs their sum to the console. The function is immediately invoked with the arguments 3 and 4.

Another way to write the same code is to use the IIFE syntax:

Text

(function(a, b) {
  console.log(a + b);
})(3, 4);

Both of these examples will output the same result: 7.

In conclusion, IIFE and arrow functions are two different concepts, but they can be used together to create self-invoking functions. This can be useful in situations where you need to create a local scope for variables and functions.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC