Hey reader :) I hope you are doing well. In the last post we have learnt about functions and how they are used in JavaScript. In this post we are going to learn about arrow functions and function invokation.
So let's get started...
Arrow Functions
Arrow functions are concise form to define functions in JavaScript. They are introduced in ECMAScript 2015.
They provide a more concise syntax compared to traditional function expressions and offer some additional benefits.
Syntax -:
Arrow functions are defined using a shorthand syntax, using the =>
arrow syntax.
Benefits of using Arrow function ->
They provide a concise format to define functions in JavaScript.
Implicit Return -: If the function body consists of a single expression, you can omit the curly braces
{}
and the return keyword. The expression's result will be automatically returned. This makes arrow functions particularly useful for concise one-liners.
There are more benefits of using arrow function but we will see them in upcoming blogs as we need to learn more prior to these.
Function Invocation
The code inside the function will execute when "something" invokes (calls) the function:
When an event occurs (when a user clicks a button)
When it is invoked (called) from JavaScript code
Automatically (self invoked)
Let's see example of each -:
When an event occurs (when a user clicks a button)
So here when button is clickedfun
function is invoked.When it is invoked (called) from JavaScript code
So here whenfun
is called then the function is executed.Automatically (self invoked)
In this example, the arrow function is immediately invoked after its definition using the parentheses()
surrounding it. This pattern is known as an Immediately Invoked Function Expression (IIFE). The function is invoked automatically as soon as the JavaScript interpreter encounters it, without needing to be explicitly called elsewhere in the code.
So this was all about this post. I hope you liked it. In the next post we are going to learn about arrays and its methods. Till then stay connected. Don't forget to follow me :)
Top comments (0)