LanguagesJavaScriptWhat's A Function?

What’s A Function? content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Hello, Tip Crew…

We’re underway with a new script. Here’s the code again to get us up to date.


<SCRIPT LANGUAGE="javascript">
function JemWebCONFIRM()
{if (!confirm ("YOUR MESSAGE GOES HERE"))
history.go(-1);return " "}


In this Tip we’re going to focus on that “function” up in the script. Here’s the basic concept.

There are a lot of commands in JavaScript that do things. One command, one effect. But, as you probably know by now, you can also combine a bunch of those singular events into a secondary output. For instance, you can use a getDate() method to grab the date, then use another series of JavaScript commands to change the numeric value of the date into text, and finally post that text to the status bar. Generally, that would be done with three different commands, and you can certainly write it that way.

But wouldn’t it be nice if you could set up those three commands and assign a title to it? One command would enact all three commands to get the desired effect. Well, you can! That’s a function.

Above, the function is named JemWebCONFIRM(). See that? The format is to write the word “function” to denote that it is a function, then immediately offer a name for the function followed by the two parentheses (). The name of the function can be just about anything. The only function names usually frowned upon are those that already exist in the JavaScript language.

Okay, so you’ve named it. Now what’s it going to do? Look again at the script above. Notice that right after the function name are some commands inside of those fancy brackets {}. That’s what it will do. Whatever appears within the fancy brackets is what will occur when you call for the function. In this case, an alert window will pop up and do a few other things, but that’s another Script Tip…

Next Time: What’s With The Parentheses, Anyway?

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories