Day 6 of #30daysOfJs

ยท

3 min read

Hey developer, hope you are doing amazing ๐Ÿ™‚.

Today is 6th day if #30daysOfJs โœŒ

So, today we are going to look at Functions.

A very important topic and widely used not only Js but also all the other frameworks or libraries associated with it.

Let's goo ๐Ÿš€

Functions

Functions are widely used in Js. One has to be very good at it to master JS.

Let's see by how many types we can define a function in JS.

So, as you can see, there are three types shown in the picture.

We will look at each and every type today ๐Ÿ™‚.

What is a function?

A Function is a reusable block of code that is designed to perform certain tasks.

A function has majorly two parts :

  1. Function declaration/ definition: This basically is a place where we write the steps that a function is supposed to do.

    A function is declared using the `function` keyword followed by a name for it. Also, it has parentheses, it can either be empty or have some parameters.

  2. Function call: A function after declaration needs to be called in order to do its job, so now we can call as many times as we want.

The function takes some parameters, and they are then called arguments.

A function makes code: reusable, efficient, clean and easy to read and easy to test.

Function with parameters and return

The parentheses beside a function can either be empty or can have multiple parameters

Function with multiple/unlimited parameters

This is for situations when we don't know how many arguments are included in a function.

therefore, we use a function that takes no parameter but inside the body with an 'argument' object, this argument object can be used to access all the parameters.

Anonymous function

A function that has no name.

Do not worry about setTimeout(), we'll soon be discussing that too.

Expression function

It is a variable that is assigned an anonymous function

The function call is done by calling the variable name and if parameters are required then add that too in the parentheses.

Arrow functions

An arrow function is an alternative to write a function, however, function declaration and arrow function have some minor differences.

Arrow function uses arrow instead of the keyword function to declare a function. Let us see both function declaration and arrow function.

Function with default parameters

The parameters are set to some value in the function definition.

Now if you just call the function with 0 parameters, it will take that default value and gets the job done

Yeah, you can change the default parameter to your own by passing an argument to it.

Higher order functions

So, it has two types

  1. They pass the function as an argument to another function.

  2. The second one is, it returns a function.

Return a function

The calling of these kind of function is much different.

Function as a parameter

The function is sent as a parameter to another function -> It is also called a "CALLBACK" function.

Unless the callback executes, the wrapper function i.e., the outer function can't be executed.

Folks, this is the end of the blog, this time I tried to draw and share those images here for a better understanding of concepts ๐Ÿฅฒ.

Hope you people will like it ๐Ÿ™‚.

That's it for today.

Goodbye ๐Ÿ‘‹

Did you find this article valuable?

Support Mubashir Ahmed by becoming a sponsor. Any amount is appreciated!

ย