JavaScript Tutorial/Language Basics/Variables

Материал из Web эксперт
Перейти к: навигация, поиск

Assign one variable to another variable

<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
yourname = prompt("What is your name?","Type your name here ...");
alert(yourname);
yourname2 = yourname;
alert(yourname2);
yourname = prompt("Type something different in","Something different ...");
alert(yourname);
alert(yourname2);
//  -->
</script>
</head>
<body>
</body>
</html>


Braces indicate code blocks

Code blocks indicates a series of statements that should be executed in sequence.

Code blocks are enclosed between an opening brace ({) and a closing brace (}).



if (test1 == "red") {
   test1 = "blue";
   alert(test1);
}


Declare two variables in the same line

<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
var msg1 = "Hello there", num1 = 22;
alert(msg1);
alert(num1);
//  -->
</script>
</head>
<body>
</body>
</html>


Define variable and assign value

<html>
<head>
<title>Variable Demo</title>
<script language="javascript" type="text/javascript">
<!--
var myVar;
myVar = "a string";
alert(myVar);
//-->
</script>
</head>
<body>
<h1>Variable Demo</h1>
</body>
</html>


Do simple calculation

<HTML>
<BODY>
<SCRIPT language="JavaScript">
<!--
var myVar=2000;
document.write(myVar+"<BR>");
myVar+=2000;
document.write(myVar+"<BR>");
myVar=myVar-500;
document.write(myVar+"<BR>");
myVar=myVar*0;
document.write(myVar+"<BR>");
myVar=myVar+500;
document.write(myVar+"<BR>");
myVar-=80;
document.write(myVar+"<BR>");
//-->
</SCRIPT>
</BODY>
</HTML>


Naming Variables

The first character of the name must be a letter or an underscore (_).

All characters following the first character can be letters, underscore, or digits.

Letters can be either upper or lowercase.

JavaScript does distinguish between the two cases.

All the following variable names are legal:



var test;
var $test;
var $1;
var _$te$t2;


Reassigning value to Variables

<html>
<head>
<title>Reassigning Variables</title>
<script language="javascript" type="text/javascript">
<!--
var myVar = 3;
alert("Variable myVar contains the value: " + myVar);
myVar = 2;
alert("Variable myVar contains the value: " + myVar);
myVar = 1;
alert("Variable myVar contains the value: " + myVar);
myVar = "Go!";
alert("Variable myVar contains the value: " + myVar);
//-->
</script>
</head>
<body>
<h1>Reassigning Variables</h1>
</body>
</html>


Reference a variable without declaration

<html>
<head>
<title>A Simple Page</title>
<script language="javascript">
<!--
yourname = prompt("What is your name?","Type your name here ...");
alert(yourname);
yourname2 = yourname;
alert(yourname2);
yourname = prompt("Type something different in","Something different ...");
alert(yourname);
alert(yourname2);
//  -->
</script>
</head>
<body>
</body>
</html>


Variables

Variables in JavaScript are defined by using the var operator (short for variable), followed by the variable name, such as:



var test = "hi";


Variables can hold different types of values at different times

A variable can be initialized with a string value, and later on be set to a number value.



var test = "hi";
alert(test);
test = 55;
alert(test);


Variables don"t have to be declared before being used

When the JavaScript sees a declared identifier (var varName), it creates a global variable.



var sTest = "hello ";
sTest2 = sTest + "world";
alert(sTest2);


Working With JavaScript Variables

<HTML>
<HEAD>
<TITLE>
Working With JavaScript Variables
</TITLE>
</HEAD>
<BODY>
<H1>Working With JavaScript Variables</H1>
<SCRIPT LANGUAGE="JavaScript">
<!--
   var numberOfDaysToSummer
   numberOfDaysToSummer = 119
   document.write("Number of days until summer: " + numberOfDaysToSummer + ".")
// -->
</SCRIPT>
</BODY>
</HTML>