Home » Online Computer Education » Learn Working with Variables in Java Script, Lesson 13

Learn Working with Variables in Java Script, Lesson 13

 

Working with Variables
I have to go over a few more things relating to variables before we move on. These important aspects of variables include the rules that govern the naming of variables and how to define variable scope.

Rules for Variable Names
Keep the following rules in mind when creating variables:

Variable names can consist only of uppercase and lowercase letters, the underscore character, and the numbers 0 through 9.

Variable names cannot begin with a number.

JavaScript and JScript are case sensitive. If you declare a variable with the name totalCount, you must refer to it using the exact same case throughout your script.

Variable names cannot contain spaces.

You cannot use any reserved words as variable names. Refer to Appendix C, “JavaScript and JScript Reserved Words,” for a list of JavaScript and JScript reserved words.

The following list is not a set of rules but is a set of guidelines you may want to follow when working with variables:

JavaScript and JScript are considered to be a loosely typed languages because they do not force you to define variables before using them. However, using the var keyword makes your code easier to understand.

You should create variable names that describe their contents. For example, lastName is a much better variable name than ln. Variable names of only a few characters may be easier to type and may seem quite obvious when you are writing your scripts, but they may not be so obvious to you a year later or to someone else who is trying to read your script.
Defining Variable Scope
You can create variables that can be accessed by JavaScripts located throughout an HTML page or from any location within a JScript. Alternatively, you can create variables that can only be accessed within the constraints of a small section of code known as a function. Defining the space in which the variable is effective is known as defining the variable’s scope. A variable can be either local or global in scope.

Local Variables
A local variable is one that is declared explicitly using the var statement inside a function. A function is a collection of script statements that can be called by another script statement to perform a specific action.

For example, the following code shows a function called ShowNotice() that displays a message every time it is called. This function uses the alert method belonging to the window object to display a text message specified by a local variable named textMessage, which has been set to “Are you sure you want to quit?”. Because this variable is local in scope, any statements located outside the function where it is defined cannot reference it.

function ShowNotice()
{
var textMessage = “Are you sure you want to quit?”;
window.alert(textMessage);
}

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published.