Variables


JavaScript Variables

A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value.

Rules for variable names:

NOTE:Because JavaScript is case-sensitive, variable names are case-sensitive.

A variable called x and a variable called X are two different variables.

Declaring JavaScript Variables

You can create a variable with the var statement:

var custname = some value

You can also create a variable without the var statement:

custname = some value

Assigning a Value to a Variable

You can assign a value to a variable like this:

var custname="Kesha""

Or like this:

custname="Kesha"

The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "custname" has the value "Kesha".


Lifetime of Variables

When you declare a variable within a function, the variable can only be accessed within that function. When you exit the function, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different functions, because each is recognized only by the function in which it is declared.

If you declare a variable outside a function, all the functions on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed


Adding Comments to a JavaScript Program

When you create a program, whether in JavaScript or any other programming language, it is considered good programming practice to add comments to your code. In this section, you will learn how to create JavaScript comments. Comments are nonprinting lines that you place in your code to contain various types of remarks, including the name of the program, your name and the date you created the program, notes to yourself, or instructions to future programmers who may need to modify your work.When you are working with long scripts, comments make it easier to decipher how a program is structured.

JavaScript supports two kinds of comments: line comments and block comments.A line comment hides a single line of code. To create a line comment, add two slashes // before the text you want to use as a comment.The // characters instruct the JavaScript interpreter to ignore all text immediately following the slashes to the end of the line. You can place a line comment either at the end of a line of code or on its own line. Block comments hide multiple lines of code.You create a block comment by adding /* to the first line that you want included in the block and you close a comment block by typing */ after the last character in the block


Hosted by www.Geocities.ws

1