DEV Community

john
john

Posted on

2 2

(function(){....})() Did you Know what is this

this function is called Immediately Invoked Function Expression(IIFE)
what's the use of this function

  • well this function get invoked itself at the time of load and we can do any ui process at that time let's say if i want to get data from localstorge at the time of load to find the theme used by the user let's take this eg.
(function () {
    const value = localStorage.getItem('theme')
    if (value === 'Dark Mode') {
        darkMode()
//darkMode is outside IIFE
    }
    else if (value === 'Light Mode') {
        return null
    }
    else {
        localStorage.setItem('theme', 'Light Mode')
    }
})()
Enter fullscreen mode Exit fullscreen mode

this code initially check theme and if it is dark calls another function in the script if it is light it returns null and if value is not present then create a theme useful in next load

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay