DEV Community

Geo Mukkath
Geo Mukkath

Posted on

1

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

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay