Javascript Functions() In Brief:-

Photo by Joan Gamell on Unsplash

Javascript Functions() In Brief:-

A very brief and short way of understanding how the functions() in Javascript works.

Functions:

We write Functions() to minimize writing the same block of code again and again.

Let's take an analogy:-

Suppose we have a robot in our home, which generally do all of our work. Be it washing clothes, buying groceries, buying vegetables, and all.

Let's say, we buy a packet of milk every day from a nearby store. Previously we go there by ourselves, but now we have our own robot to do this work for us.

So, we sent our robot to a nearby milk store and give him/her these instructions before leaving the house.

1)Leave the House

2)Go to the store

3)Purchase Milk

4)Come back home

Now, This instruction is good for one time or two, But if we want our robot to go to the store every day and buy milk and come back home, we are not going to give him instruction every day because that's, not the efficient way of doing it. After all, robots are here to make our life easy.........So what we do here is we write a block of code or instructions that we give our robot previously and store it inside a function called get milk (). Now, whenever we want our robot to go to the store and buy us milk instead of telling all those instructions we just call our getMilk() function and our commands or instructions or Code gets executed. How we insert that code inside our robot is a whole different thing so don't get yourself confused here. It's just an analogy to tell how functions() work.

Think of it like a game, where you shoot zombies by tapping on the shoot button. We just tap on the shoot button and then on the backend side, a code gets executed and then the shooting happens but we only see that shoot happens when we tap on the shoot button, as simple as that.

Creating a function:-

Function getMilk() { Block of Code }

  • Inside the Block of code, we write all the instructions which we want our robot to do for us.

  • Now, whenever we want milk, we just call that function & our code gets executed automatically.

Calling a function:-

getMilk();

This calls the function we created & our code gets executed.

Did you find this article valuable?

Support Shashank Sharma by becoming a sponsor. Any amount is appreciated!