PGTUTOR.COM
You are here: HOME >> JavaScript tutor >> Lesson 2

JavaScript tutor- Lesson 2

If you ever took "advanced math" you know what a function is for. A function is used when there's a repetition. For example, the formulae: y=(x+67)*32.5 is a function. If you give the function a value for x, it will give you a value for y.

For now all you need to know is that in the above function x is a variable. A variable can have any value. It can be 5, 6, 7, 1000 etc. It doesn't have to be a number. In some cases it can be something like "John".

Consider the following:

<html>
<head>
<title></title>

<script type="text/javascript">

function MyMessage() {
name="John";
alert(name);
}
</script>

</head>

<body>

<a href="javascript: MyMessage()">Say Something</a>

</body>
</html>

View your work here

In this case, name is a variable. It can have any value. Also notice that the value of a variable is enclosed in quotes but the variable itself isn't. And a semicolon (;) is used to separate one statement from another.


Now give this a shot:

<html>
<head>
<title></title>

<script type="text/javascript">

function MyMessage() {
name="John";
alert("My name is " + name);
}
</script>

</head>

<body>

<a href="javascript: MyMessage()">Say Something</a>

</body>
</html>

View your work here

Notice the use of + to connect a variable with some text. What If I wanted to add an exclamation point after the statement?

<html>
<head>
<title></title>

<script type="text/javascript">

function MyMessage() {
name="John";
alert("My name is " + name + "!");
}
</script>

</head>

<body>

<a href="javascript: MyMessage()">Say Something</a>

</body>
</html>

View your work here

Don't move on to the next lesson until you understand what's going on here.

PgTutor - The easy way to make a website ...
Lessons: Intro 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25



PGTUTOR is an award winning website. In the past 2 years, we've won over 20 legitimate awards. Go to our awards page to verify for yourself.




You're not taking a risk when buying our products. If for any reason you're unsatisfied with the quality of our product(s), we'll refund your money. Also, you get to keep the product you bought.