DEV Community

Geo Mukkath
Geo Mukkath

Posted on

How would you explain JavaScript functions to a 5 yr old ?

JS Functions

Say you need someone to fix your pipes at home.
Whom would you call ?
A plumber!

What does a plumber do?
He fixes pipes, drains and deals with everything related to your water connection.

Say you need someone to teach you math.
Whom would you call ?
A math teacher!

What does a math teacher do ?
She teaches everything relating to math like adding, subtracting , dividing and all the other good stuff.

Say you need a
.
.
.
.
.
.
.
.
You get the point.

In JavaScript a function is a reusable piece of code that does a very specific task and it can be called anytime during the execution of the program.

So in the case of our plumber it would look something like this

function plumber(){
console.log('I am here to fix your commode');
}

plumber();
Enter fullscreen mode Exit fullscreen mode

What I have done above is called as a function declaration.
I have declared a function called plumber in line 1 and subsequently in line 2 the function does whatever it is supposed to do.

Declaring a function is not enough. Functions are reusable, hence we can call them anytime we need them.

Back to our scenario. The plumber for whatever reason has forgot to bring his equipment to work. Fortunately you have a spanner, hammer and a plunger at home. You give him the equipment.

In programming it's called 'Passing the arguments'

How would you do that? Simple. Pass them when you call the plumber.

plumber(🔧,🔨,🪠)

He will use the above equipment to fix your commode.

function plumber(🔧,🔨,🪠){
🔧 + 🔨 + 🪠= 🚽
console.log('I am here to fix your commode');
}

plumber(🔧,🔨,🪠);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)