Welcome Guest! (App Version 3.1)

websitnero logo name

Anonymous Functions

What are Anonymous Functions in JavaScript?

Anonymous Functions are called so because they aren’t given a name in the same way as normal functions. Thus, they are functions that do not have name and cannot be called at will.

However, you might create an anonymous function and assign a variable to it and you may call the anonymous function with the help of this variable, but still that function will be anonymous.

The main use of anonymous function is for a certain code block that must run once and then should not be accessible or called again, even if someone wants to. Most often this is done so that hackers or miscreants do not take advantage and go rouge with important information or sensitive data.

The best method to achieve this is by turning anonymous functions into IIFE.

Example

<script>

//Named Function
function alpha(){
  alert('hi');
}

//Writing function without name - Anonymous Fn 
//would give error as parser expects a name

/*
function (){
  alert('hello');
}
*/

//If Anonymous Function is immediately infoked 
//as IIFE - parser will not give error 
//and function will run instantaneously once

(function (){
  alert('good day');
})();

alpha();

</script>
WebsiteNero is highly optimized for learning various website developing technlogies. We try our best to add maximum modules and examples to help learn the concepts clearly and vividly. We try to present all content and examples as simple as we can removing any complexity to hurdle easy understanding. We try best provide you with worthful content, but we cannot guarantee full correctness of all the content on this site. While using this website, you agree to have read and accepted all our terms of use and conditions, cookie, and privacy policy. Copyright, 2013-2018 by websitenero.com. All Rights Reserved.