Exforsys

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction

Ads


Home arrow Technical Training arrow JavaScript Tutorial

JavaScript Variables

Author : Exforsys Inc.     Published on: 15th Apr 2007

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.

Ads

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>

Ads

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>



 
This tutorial is part of a JavaScript Tutorial tutorial series. Read it from the beginning and learn yourself.

JavaScript Tutorial

  1. JavaScript Browser Objects Part 2
  2. JavaScript Frame object
  3. JavaScript Form Object
  4. JavaScript FileUpload Object
  5. JavaScript Event Object Properties and Methods
  6. JavaScript Event Object
  7. JavaScript Elements and Embed Objects
  8. JavaScript Applet Objects
  9. JavaScript Browser Objects
  10. JavaScript Object Oriented Features
  11. JavaScript Window Object Open Method Part 2
  12. JavaScript Window Object Open Method
  13. JavaScript Window Object Timeout Methods
  14. JavaScript Location Object
  15. JavaScript Location Object Properties
  16. JavaScript History Object Properties and Methods
  17. JavaScript Document Object Methods Part II
  18. JavaScript Document Object Methods Part I
  19. JavaScript Document Object Properties
  20. JavaScript Document Object
  21. JavaScript Windows Object Properties Part II
  22. JavaScript Windows Object Properties Part I
  23. JavaScript DOM Window Object
  24. Working with JavaScript DOM Objects
  25. JavaScript Array Object Methods – Part II
  26. JavaScript Array Object
  27. JavaScript Array Object Methods – Part I
  28. JavaScript Boolean Object
  29. JavaScript OnError Event
  30. JavaScript Exception Handling – Part II
  31. JavaScript Exception Handling – Part I
  32. JavaScript Event Handler
  33. JavaScript Events Handling
  34. JavaScript Array Operations
  35. JavaScript Two Dimensional Arrays
  36. Passing values to JavaScript Function
  37. JavaScript Functions
  38. JavaScript Arrays
  39. JavaScript Iterative Structures - Part II
  40. JavaScript Iterative Structures - Part I
  41. JavaScript Math Object
  42. JavaScript Date Object
  43. JavaScript String Object
  44. JavaScript Objects
  45. JavaScript Confirm Box
  46. JavaScript Alert Box
  47. JavaScript Conditional Statements Part 2
  48. JavaScript Conditional Statements Part 1
  49. How to use JavaScript in HTML page
  50. JavaScript Variables
  51. JavaScript Features
  52. JavaScript Introduction
 

Comments