Making JavaScript Functional

by Nick Jensen on October 10, 2013

Well folks, we’re beginning to submerge into the more frustrating…err..I mean esoteric aspects of JavaScript coding. Please bare with me as I go through Chapter Four of John Pollock’s JavaScript: A Beginner’s Guide. Let me start by trying to explain functions. Pollock describes a function as “a little script within a larger script”. In other words, a function can be used to assist a script in carrying out its programming. Now, a function is useless unless you declare it. Functions begin with a script command as in the following:

function myfunction ( )

The beginning of this script tells the browser to execute the function that’s listed. In this case, it’s “myfunction”. But why are there parentheses after the function? Those parentheses are reserved for arguments. No, that doesn’t mean we reserve them for a hostile exchange of words. Instead, arguments represent values that originate from outside the function. If you recall from my second to last entry where we began our lesson on values and variables, I created a value named “mybrandnewcar”. It’s a value that exists outside the function. By adding it inside the parentheses, it becomes a part of the function:

function myfunction (mybrandnewcar)

Of course, you can put multiple arguments into a function:

function myfunction (mybrandnewcar, myjunkcar)

Just make sure you separate each one with a comma. Also, as Pollock points out, never use the variable keyword (var) when adding a value to an argument. The argument automatically assumes that any value outside the function is a variable. Now that we know a little bit about functions and arguments, let’s create one of our own. We’ll start by including the two variables that I wrote above.

var mybrandnewcar = “I want a Volkswagen”;

var myjunkcar = “my Nissan Altima looks like it has Eczema!”;

Next, let’s write the function itself. When creating a function, you must always put all of it’s code inside brackets as demonstrated below:

function mycarproblem (mybrandnewcar, myjunkcar)  {

document.write(mybrandnewcar + “because” + myjunkcar);


On a web browser, the above script would translate into “I want a Volkswagen because my Nissan Altima looks like it has Eczema!” No, really! It does. The finish looks like it’s exfoliating. Anyway, enough about my car. Okay, so we know what functions are and what they do, but where do we put them? JavaScript functions can be placed anywhere inside an HTML file. Although, Pollock recommends storing functions in an external file in order to keep the HTML code separate from the JavaScript code. Pollock also advises JavaScript coders to place the script tags right before the closing body tag. Doing so will ensure that the page can finish loading before it executes any JavaScript commands. Right now, we are halfway through Chapter Four and since this is an exceptionally long chapter and there’s a lot of information to cover, I’m going to call it right here. Fear not, though. We’ll finish this chapter before you know it and then move on to Chapter Five where we will learn all about JavaScript operators. Until next time!


{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: