welcome
Help

Web Programming Primer

-Lesson 14-







Alert and Functions

Alert

When you use an alert, a separate message window will open up on screen with any message you give it. This is not necessarily useful for a website, but will help aid us in our learning of JavaScript. To perform an alert, simply type alert followed by a set of parenthesis and a semi-colon. Inside of the parenthesis is where you would type your message (in quotes of course!).

Like this:


Functions

Functions are mini programs that you can insert inside of your script. These are programs that you can code out, but won't be performed until you specifically call them into action. These are useful when you have an action that will be performed over and over, so you can just continuously call the same function every time you need it. An example of a useful function is every time you click like on facebook, it adds a thumbs up.

To code a function, type "function" followed by what you want to name the function, and then type a set a parenthesis (which are where you insert arguments) and then a set of curly brackets (this is where you will put all your javascript function code).

Like this:




In the example above, "hello" is the name of the function. We will leave the parameter space empty for now, but let's add a simple line of code that we just learned in the curly brackets. We will insert an alert, which will say "hi." We will also call the function. The function will not do anything until it is called, so, to see it in action, we will call it. To do this, we must type the function's name, followed by a set of parenthesis, and then a semi-colon.

This is what it looks like:





Test this out for yourself. When you load the page, the word "hi" should pop-up in a message box.



Here is another example, using an if statement inside the function, to check if the name AND age match before issuing an alert box:









Parameters

Parameters on functions are used to pass information through to the function. This is so that you may utilize a function over and over, while using different data everytime. When you call the function, you will put the data or variable you want to pass through to the function inside the parenthesis. The function will then take that data and run it through in the place that you have set it. If you want to pass more than one, just put commas in between them.

Here is the last example redone, using parameters:








As you can see above, there are two parameters used. The ones in the call method are termed arguments. They are the variables that you wish to pass through for the function to use. When you make your function, and give it parameters, you declare the parameters like variables (you simply type a name for the variable) and then use that new name when using it in the function. This may not seem useful now, but it actually is. One reason this is a good approach when using functions is that you can change the value of the variable you pass through without changing the value of the original variable. For instance, let's say we want to take the user's name and change it to "sir ____" without ruining the original variable's value (because we may need it later on for something else).

It could look like this:








Above, the variable name was passed through and it's value was given to a new variable "b." b was then changed to equal "sir " plus itself. Then it was placed in an alert message. This allowed for a manipulating the value of name without changing the original variable itself.


Lesson: 11 12 13 14 15

Back Next
This page was constructed by Isaac Duke, a student of Anderson University, for an independent study in web programming. This is not a fully comprehensive primer, so use in conjunction with other resources (resource page provided). The last time this site has been updated was on August 20, 2013. If any questions, email misaacduke@gmail.com