View Full Version : Variables and Functions
-iNsOmNiAc-
11-05-2002, 03:13 PM
Can someone explain what variables and functions are in JScript? I'm starting to read tutorials, the tutorials do an excellent job explaining what the scripts do, but fail to give a definition.
http://www.pageresource.com/jscript/jvar.htm
kdjoergensen
11-05-2002, 05:11 PM
A variable is storage container which can hold information, such as strings, numbers, image references, objects etc
The information can be changed, thus the term VARIABLE rather than static.
examples:
var myDog = "Rofus"
myDog is a variable to which we have assigned a text value (a text string). The string value is "Rofus".
You assign or change values by setting the variables equal to the values. You declare the variables using 'var' keyword (although global variables usually do not require declaration in javascript -- optional).
E.g.
declare variable:
var myVar;
assign value:
myVar = "20";
change value in variable;
myVar = "15";
declare and assign in same statement:
var myNewVar = "35";
declare two values:
var myVar;
var myOtherVar;
Assign two values:
myVar = 14;
myOtherVar = 28;
Create and assign a 3rd value based on the other two:
var Result = myVar + myOtherVar;
Hope variables were clear !!
Functions in javascript are typically like sub-routines, e.g. isolated program blocks which can be called, or activated, by calling them by their name after the fact.
example:
function runLater(){
alert("I am running after ONE");
}
alert("I am ONE -- I run first");
runLater(); // function call
In above example, although the alert statement "I am running after ONE" is defined early in the script, it has been encapsulted inside a function statement (defined by the funny brackets: { } ) In the script above the intial alert statement is outputted: "I am ONE -- I run first" and then a function call is made to the funLater function. We call the function by calling it by it's name (remember the two - normal - brackets after then name)
You can also pass a value to the function:
function runLater(note){
alert("HI - you wrote: "+note);
}
runLater("my message here");
In above example the message "my message here" is passed as parameter to the function through the function call. In the head of the runLater function (withion the brackets) we define a variable (you know what that is now !!!) which we have called: note which captures the message which was passed by reference. We then use the note variable to output the message via an alert statement.
I hope above gave you some ideas what variables and functions are.
Nice answer, with some good examples.
Jon Hanlon
11-05-2002, 09:02 PM
w3schools.com have a good tutorial on Javascript:
http://www.w3schools.com/js/default.asp
Dr. Web
11-06-2002, 01:33 AM
www.trainingtools.com
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.