JavaScript: Functions (Uncle Roger Edition)

In this post we will be talking about Javascript Functions, using the Feynman Technique with the help of YouTube sensation Uncle Roger as part of my Feynman Technique/20 Hr Method challenge. For more information, please head here.

What are Functions

Functions are an object which contains a list of instructions that information goes into and something comes out the other end.

We’re not talking about poop, although if the result you want is ๐Ÿ’ฉ, I guess that’d be accurate.

Since functions are a list of actions, normally, they won’t run without something to activate it, and on the other side of that coin, if the directions in the list continue to loop, with no end, it can crash your browser.

Function Types

There are three kinds of functions: declaration, expression, and arrow. For the first two, the result is still the same, but the usage of each isn’t.

Here’s the declaration function:

1 function uncleRoger() {
2 console.log("Where your MSG?");
3 console.log("Drain the rice?! Haiya! ๐Ÿ˜ฅ");
4 console.log("No wok, no wok-hay.");
5 };

This is an expression function:

1 const uncleRoger = function () {
2   console.log('Where your MSG?');
3   console.log('Drain the rice?! Haiya! ๐Ÿ˜ฅ');
4   console.log('No wok, no wok-hay.');
5 };

Usually, a declaration might be unnamed when living in another function (yes, functions can live inside other functions). Kind of like that person subletting your apartment that your landlord doesn’t know about. They’re there, they pay their rent on time and do what they’re supposed to do, but nobody knows their name.

To execute either of these functions is the same as well:

6 uncleRoger();

…and the output would look like this:

2 Where your MSG?
3 Drain the rice?! Haiya! ๐Ÿ˜ฅ
4 No wok, no wok-hay.

The code “uncleRoger();” is the method we use to activate the function, and it will repeat anytime you “invoke”, “call”, or “execute” it (whichever term of “run” you prefer).

As for the arrow function, let’s get the first thing out of the way. Yes, it looks like a prepubescent DOS version of a penis (=>). You can laugh now.

Now that done, arrow functions are better for equations and array (information grouping) instructions than it is with these standard console log responses, but can still be used. They are also effective in boolean (true/false) statements.

const uncleRogerFriedRiceApproved = chiliJam =>
  (chiliJam >= 1 ? console.log('Haiya!') : console.log('Fuiyo!'));

The function above is called “uncleRogerApprovedFriedRice” and is saying if chiliJam amount in the rice is greater than or equal to 1, then Uncle Roger will be upset and say “Haiya!”, otherwise, he’ll be happy and say “Fuiyo!”.

If we were to run this, it would look like the following:

uncleRogerFriedRiceApproved(1);

…and the response to this would be:

Haiya!

I agree, Uncle Roger, not a fan of chili jam, either.

…and by the way Uncle Roger, I use chicken bullion when making my fried rice, which does contain msg.

Click on the links below for more information on Javascript Functions:

Thanks for reading!