DEV Community

Akshat Sharma
Akshat Sharma

Posted on

Day 8 of 30 of JavaScript

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 -:
Image description
Arrow functions are defined using a shorthand syntax, using the => arrow syntax.

Benefits of using Arrow function ->

  1. They provide a concise format to define functions in JavaScript.

  2. 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.
    Image description

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 -:

  1. When an event occurs (when a user clicks a button)
    Image description
    So here when button is clicked fun function is invoked.

  2. When it is invoked (called) from JavaScript code
    Image description
    So here when fun is called then the function is executed.

  3. Automatically (self invoked)
    Image description
    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)