Anonymous Function Call in javascript | Function Expression using Negation & More

preview_player
Показать описание
In this tutorial we are looking into javaScript function calls, focusing on anonymous function calls in JavaScript. We delve into function expressions using negation, a technique marked by an exclamation point (!) preceding the function in JavaScript. Discover the effects of the exclamation mark before a function, its purpose, and when best to utilize this unique technique.

Have you used function starting with exclamation mark or function expression using logical not operator? What does the exclamation mark do before the function? What is Javascript anonymous function call? Why use NOT operator on anonymous function call? what does !function in javascript mean? What is ! preceding function in javascript?

In this video we will answer following questions:
- In JavaScript, what is the advantage of !function(){}() over (function () {})()
- How exactly does !function(){}() work
- JavaScript !function(){}
- What is the purpose of declaring a function like !function(){ code }()

Learn what is anonymous function and what is named function, what is immediately invoked function expression and what is difference between a function and function expression in javascript. Learn about function expression using negation in javascript.

A function is a block of code defined once and called multiple times later. In JavaScript, a function can be declared in many ways.

for example:
function sayHello(){}
This is a function declaration. But it is not invoked yet. We would need an invocation, to actually run the function.

sayHello()

will run this function.
This is a function call.
It is undefined.
This is a named function that is being executed.
We can declare a function like this and invoke it later in code.

What if we need a function that invokes immediately after its being declared.
In this case we don't need name for this function.
If we remove name from this function.
It throws SyntaxError.

To fix this we can turn it into a function expression and use IIFE.
IIFE is an 'Immediately Invoked Function Expression'.

(function(){})()

It is a function expression that is being invoked immediately. It shows undefined at declaration point. An immediately invoked function expression (IIFE) doesn’t explicitly return anything, so its return value is undefined.

BTW we can also use arrow in IIFE.

There is another way to declare and invoke a function. It is by using the logical NOT ( ! ) operator or negation.

!function(){}()

By using this syntax, we have removed parenthesis and we are saving one byte per function expression.

NOT operator, alone can not invoke the function. We still need parenthesis at the end. Parenthesis has higher precedence than exclamation sign and it instantly calls the function.

Removing NOT operator will end up in SyntaxError, because you can't put arguments right after a function declaration. NOT operator makes the expression return a boolean, based on the return value of the function. IIFE doesn’t explicitly return anything, so its return value will be undefined, which leaves us with !undefined which is true.

And why this is used. This is usually done by minification or uglification scripts, where every single byte counts.

So if you see such function. It is just a function expression using logical NOT operator.

✅ How To Hide / Protect JavaScript Code - Techniques and Tools
✅ React JS UNDEFINED Solution | Reactjs Learning | Learn ReactJS & JavaScript Debugging Fast
✅ Calculate Sum or Total from Values in Array & Array Object | Learn JavaScript | Reduce Method
✅ Get URL Parameters in JavaScript | URLSearchParams
✅ DIV to PDF, Iframe to PDF using JavaScript Only (with CSS & Images Support)
✅ How To UnMinify JavaScript Minified Files Easily | Decrypt Complex JavaScript Code
✅ Toggle Class in Vanilla JavaScript | Learn JavaScript
✅ How to Execute a JavaScript File by using NPM
✅ Get User's Location (Country, State, City) with Free Unlimited API Access | PHP & JavaScript
✅ Replace jQuery with Vanilla JavaScript Code Easily | jQuery vs JavaScript
✅ Toggle in Vanilla JavaScript
✅ JavaScript Variable Redeclare & Reassign | VAR vs LET vs CONST | Practical Examples

Thank You!
👍 LIKE VIDEO
👊 SUBSCRIBE
🔔 PRESS BELL ICON
✍️ COMMENT

#function #functionExpression #IIFE #logicaloperator #logicalNOTOperator #NOTOperator #js #javascript #WebStylePress #WebDevelopment
Рекомендации по теме