JavaScript (Conjunction Junction) Functions

Conjunction Junction, What's Your Function?

Do you remember Schoolhouse Rock? No, not Jack Black's timeless classic School of Rock, but the animated educational series? Please say yes, otherwise my picture above and the entirety of this blog won't make any sense.

Today we're talking about functions and how to use them to simplify your JavaScript code. Functions are little programs you can use over again without changing much. For example, you can use the default JavaScript function "document.write" to write a line. If you change the parameter (the bit between the brackets), it'll give a different response.

Depending on the function, sometimes you can have two, three, five or even twenty parameters of information to add.

Conjunction Junction's Function

Conjunction Junction is a train junction is Schoolhouse Rock where train cars (which are different words) are combined with three "special" train cars (conjunction words) to form complete trains (sentences). It's fun concept and the stupid song has been ingrained into my head since 1994. Since I have to suffer because of it, so do you.

To begin, let's make and alert a sentence.

Simple enough. Now let's make a function and call it "conjunctionJunction" and put our sentence in there instead.

Functions are a little more complicated than some of the things you've seen before. They start with the word function and a response is returned at the end of it. Since we have the variable sentence calling that conjunctionJunction function, the sentence will be returned and alerted.

Adding Parameters

Now that the conjunctionJunction function has a function, we can hook up some words and phrases and clauses. (I am really sorry about this, but it's just going to get worse from here.) Let's take the word "and" out of the sentence and put it between the brackets of the conjunctionJunction function.

We're now passing the word "and" into the function as the variable word, and adding that into our sentence. Let's see what happens if we do this:

We can add variables into functions to change the outcome. It's fairly simple so far, so let's make it a little more complicated. We're going to add another conjunction word into our conjunctionJunction function.

But (see what I did there?) now it doesn't make sense. We need to do an if statement and change our sentence for this new conjunction word in our conjunctionJunction function.

Adding multiple parameters

Now that we are junctioning different types of conjunction words into our conjunctionJunction function, let's make it a little more complicated:

By adding multiple parameters, we could have a wider variety of responses come out of our function. While these examples are all based around text, we could do something with numbers as well.

Junctioning functions

These examples have been pretty simple so far, but they help show how you can feed different sets of data into a program and get different results. Whatever JavaScript script you do outside a function, you can do inside it too. This included junctioning functions inside your conjunctionJunction function with different kind of conjunctions and words in the form of an array, like so:

If you made it this far with all my terrible jokes and you have no idea what I'm talking about, here's a video that explains it.

Sign-up for inside tips for making
your website more beautiful!

* indicates required


Sign-up for inside tips for making
your website more beautiful!

* indicates required