JavaScript Variables
In this JavaScript tutorial, you will learn about JavaScript variables, rules for variable names in JavaScript, declaration of variable in JavaScript, variables usage, JavaScript in external file, how to place the JavaScript in external file and how to execute this JavaScript placed in external file.
Just like any programming language, variables in JavaScript are also used to store values. The value of the variable can be edited as required by the programmer.
Rules for Variable Names in JavaScript:
Some of the rules for forming variable names are as follows:
- Variable names are case sensitive.
- They must begin with a letter or the underscore character
Now let us see how to declare a variable name.
Declaration of Variable in JavaScript:
This is done by using the var statement. The syntax of var statement is
var variable name = value
In any programming language, a variable must first be declared. Storage location must be allocated for the variable before using the variable.
In JavaScript, it is possible to skip the command var and directly assign value to variable names. For Example:
variable name = value
Using the variable name Exforsys and the notion that a programmer wants to assign the value “Training” to this variable it can be written two ways:
var Exforsys = “Training”
or It can also be done as follows:
Exforsys = “Training”
Variables Usage:
The variables can be declared and used in two ways namely:
- Locally
- Globally
When a programmer declares a JavaScript variable within a function, the variable can only be accessed within that function. When the control moves out of the function, the variable is destroyed. These variables are called local variables. Each of the local variables placed with the same name in different functions are different because they are recognized only within the function in which they are declared.
If a programmer declares a JavaScript variable outside a function, all the functions on the page can access the variable. The lifetime of these variables starts when they are declared, and ends when the page is closed.
JavaScript in External File:
There may be scenarios in which the same functionality of script needs to be executed in several places in the program. Instead of writing the same JavaScript in several places (which would cause poor optimization of code) the programmer can place the JavaScript in an external file thereby allowing programmers to make use of single external file at more the one place.
How to place the JavaScript in External file:
This process is very simple. The code or the JavaScript that needs to be executed in several places in the program is written separately in a file and then saved with extension as .js for that file.
How to execute this JavaScript placed in External File:
This is performed using the attribute src for the <script> tag.
The script tag is placed as needed in either the HEAD section or the BODY section using the src attribute as follows:
<html>
<head>
<script src="filename.js"></script>
</head>
<body>
</body>
</html>
Suppose the JavaScript is placed in an external file named Exforsys.js then it is executed by placing it as:
<html>
<head>
<script src="Exforsys.js"></script>
</head>
<body>
</body>
</html>