Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,294 members, 7,836,286 topics. Date: Wednesday, 22 May 2024 at 02:53 AM

Javascript Self-executing Functions - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Javascript Self-executing Functions (160 Views)

Inline Functions In C++ / We Develop Website And Apps + Full Functions: Check In / After 10days Of Coding With Html, Css And Javascript Forum4africa Is Ready (2) (3) (4)

(1) (Reply)

Javascript Self-executing Functions by kemi72: 1:08am On Oct 10, 2023
What are Self-Executing Functions?
A self-executing function, also known as Immediately-invoked Function Expression (IIFE), is a JavaScript function that is defined and executed immediately after its creation. Unlike regular functions that require a call to execute their code, IIFE runs automatically. This feature is particularly handy when you want to encapsulate code and create a private scope for your variables, preventing them from polluting the global scope and is a good software development practice.

The basic syntax for an IIFE looks like this:

(function () {
// Your code here
})();
Here’s a breakdown of how this works:

Enclose the function definition within parentheses (function () { ... }).
Immediately following the closing parentheses, add another pair of parentheses ( ... ) to invoke the function.
In this software development training in Abuja, Nigeria, you will learn how to build software solutions that solve real time problems.

Inside the first set of parentheses, you can pass parameters to the IIFE just like you would with any regular function. For example:

(function (x, y) {
console.log(x + y);
})(5, 10); // Outputs: 15
Benefits of Self-Executing Functions
Self-executing functions offer several advantages that make them a valuable tool for software developers who use JavaScript:

1. Encapsulation and Scope Isolation
One of the primary benefits of IIFE is that they create a private scope for your code. Any variables declared inside an IIFE are not accessible from the outside, preventing them from conflicting with variables in the global scope or other parts of your code. This helps improve code maintainability and reduces the risk of naming collisions.

(function () {
var privateVar = 'This is private';
console.log(privateVar); // Outputs: 'This is private'
})();
console.log(typeof privateVar); // Outputs: 'undefined'

Read more
https://codeflarelimited.com/blog/javascript-self-executing-functions/

(1) (Reply)

Tools For Creating A Drupal Website Documentation / I Have 11years Old Facebook Account / Cost And Features To Build An On-demand Food Ordering App

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.