Better Programming

Advice for programmers.

Follow publication

Member-only story

JavaScript: Normal, Anonymous, and IIFE Functions

Dallas Bille
Better Programming
Published in
5 min readMar 20, 2019

--

Nine days of full-time JavaScript. Coming from Ruby, I had totally forgotten how to define a function in JavaScript (I did have some experience before, all of which was forgotten).

Luckily, some of the concepts are the same as Ruby, which is the language I had learned in the six weeks prior. I look back at those days fondly. The simplicity. The flexibility. A syntactic breeze!

I can only compare JavaScript to Ruby, I only know Ruby. Coming from Ruby to Javascript is a frustrating transition at first (albeit far, far easier than coming at a programming language with nothing).

Functions

Allow me to describe why functions are useful and what they do.

Analogy: You work at a super busy restaurant. Super busy. All they serve is lasagna. It’s your job to heat up each slice of lasagna in the microwave for one minute.

You have to do 59 slices an hour. And punching in one, then zero, then zero again, then start is eating up too much time. And your boss, Guido Salami, is getting irritated because you’ve only produced 58 slices in the last hour.

Then you notice a button on the microwave, it says one min. As you are working a job well below your cognitive ability, you deduce that this button is preprogrammed for one minute and starts when pressed.

So, now all you have to do is put in a cold slice, close the door, and press one min. You produce 60 slices of lasagna that next hour, impressing Guido with your innovation, he offers you a raise and promotion, and you turn it down to become a JavaScript developer.

This is what a function does. It performs repetitive tasks on data for you, so you don’t have to type out the behavior every time you need it.

What a fun analogy.

Basics and Normal/Declared Functions

The first thing I noticed whilst learning JavaScript is some general rules while writing a function. The first being, you have to let JavaScript know that it is a function.

--

--

Dallas Bille
Dallas Bille

Written by Dallas Bille

Full Stack Web Developer, Adventurer, Soccer Player/Coach

Responses (3)

Write a response