In previous demonstrations we have used document.write() or document.writeln(). The alert() method is a great debugging tool. The alert() method is a great way to find out what is in a variable. It pops open a box with the value you want to display. So lets take a quick look at the alert() method.
The built-in JavaScript alert() method displays a popup dialog box with an OK button. The alert() method accepts string literals and variables, or any combination of the two.
alert("this is a string"); using a string click here to see demo<Script language="JavaScript"> var myVar = 19 alert(myVar); </Script>Click here to see the example
function printLine()
{
document.write("<hr>");
}
To call the function, place the function name between script tags like this:
function printColorLine(lineColor, lineSize)
{
document.write("<hr color=" + lineColor + " noshade size=" + lineSize + ">");
}
function totalItems(num1, num2, num3)
{
var sum_numbers = num1 + num2 + num3;
return sum_numbers;
}
function mathProblem()
{
document.writeln(n1);
document.writeln(n2);
document.writeln(n3);
document.writeln("---");
var problemTotal = totalItems(n1,n2,n3);
document.writeln(problemTotal);
}
Click here to see the exampleglobal variables are variables that are defined outside of a function. Therefor everyone can see them. Local variables are created in a function and can only be seen by that function. When the function ends the variables are gone.
//global varible
var item = "pants";
function displayItem(){
document.write("item=" + item);
}
In the above example the function can see the variable item, because it is a global variable.
Click here to see the exampleIf the function created a variable named item, the function would no longer see the global variable item. The function could set the variable to a different value and it still would not effect the global variable.
//global varible
var item = "pants";
function displayItem(){
//local variable
var item="shirt"
document.write("item=" + item);
}
In the example above, the function displayItem() creates a variable with the same name as the global variable. So, the function displayItem() uses the local variable, sets it to the value of "shirt". This does not change the global variable item, it stays set to the value of "pants". So, the function displayItem{} only uses the local variable. Later in the body of the document you could display the variable item, and it would still have the value of "shirt".
Click here to see the exampleWe could change the function to accept a parameter, pass the variable item to the function. Notice when we call the function using dispalyItem(item) inside the function it becomes myStuff.
//global variables
var item
function displayItem(myStuff){
//local variable has same name as global variable - item
var item = "shirt"
document.write("<br>in function displayItem, local variable item=", item);
document.write("<br>in function displayItem, variable myStuff=", myStuff);
}
.
. later in the program we call the function and pass the global variable item to the function....
.
displayItem(item);
Click here to see the example