JavaScript Variables

Like many other programming languages, JavaScript has variables, variables can be thought of as named containers. You can place data into these containers and then refer to the data simply by naming the container. Before you use a variable in a JavaScript program, you must declare it.

Variables are declared with the var keyword as follows:

<script type=”type/javascript”>

<!— var money ; var name; //-- >

</script>

You can also declare multiple variables with the same var keyword as follows:

<script type=”text/javascript”>

<!— Var money, name; // -- >

</script>

Script a value in a variable is called variable initializations. You can do variable initialization at the time of variable creating or later point in time when you need that variable as follows: For instance, you might create a variable named money and assign the value 2000.50 to it later.

Learn JavaScript by Tekslate - Fastest growing sector in the industry. Explore Online "JavaScript Training" and course is aligned with industry needs & developed by industry veterans. Tekslate will turn you into JavaScript Expert.

 

For another variable you can assign a value the time initialization as follows :

<script type=”text/javascript”>

<!— var name=”Ali”; var money; money=2000.50; //-- >

</script>

Note: Use the var keyword only for declaration or initialization. once for the life or any variable name in a document. You should not re-declare the same variable twice.

<script type=”type/javascript”>

<!— functionsayHello() { alert { “Hello World”} } //- - >

</script>

</head>

<body>

<script type=”text/javascript”>

<!— document.write(“Hello World”) // - - >

</script>

<input type=”button” onclick=”sayHello()” value=”say Hello” />

</body>

</html>

 

JavaScript in External File

As you begin to work more extensively with JavaScript, you will likely find that there are cases where you are reusing identical JavaScript code on multiple pages of a site. You are not restricted to be maintaining identical code in multiple HTML files. The Script tag provides a mechanism to allow you to store JavaScript in an external file and then include it into your HTML files. Here is an example to show how you can include an external javaScript file in your HTML code using script tag and its srcattribute :

<html>

<head>

<script type=”text/javascript” src=”filename.js”>

</script>

</head>

<body> ……………….. </body>

</html>

The use of JavaScript from an external file source, you need to write you all JavaScript source code in a simple text file with the extension “.js” and then include that file as shown above.

For example, you can keep following content in the filename.js file, and then you can use the say Hello function in your HTML file after including filename.js file: functionsayHello() { alert(“Hello World”) }  

JavaScript Data Types

One of the most fundamental characteristics of a programming language is the set of data types it supports. These are the type of values that can be represented and manipulated in a programming language. Here are simple steps to turn on or off JavaScript in you Firefox

  1. Follow Tools - > Options from the menu
  2. Select the Content option from the dialog box
  3. Select Enable JavaScript checkbox
  4. Finally, click on ok and come out.

  To disable JavaScript support in your Firefox. You should not select Enable JavaScript checkbox.  

JavaScript Variable Scope

The scope of a variable is the region of your program in which it is defined. JavaScript variable will have only two scopes.

Global Variable:

A global variable has a global scope which means it is defined everywhere in your JavaScript code.

Check Out JavaScript Tutorial

Local Variable:

A local variable will be visible only within a function where it is defined. Function parameters are always local to that function Within the body of a function, a local variable takes presentence over a global variable with the same name.

If you declare a local variable or function parameter with the same name as a global variable, you effectively hide the global variable. The following example explains it:

<script type=”text/javascript”>

<!— varmyVar=”global”; // Declare a global variable functioncheckscope() { varmyvar=”local”; // Declare a local variable document.write(myVar) ; } //-- >

</script>

JavaScript Variable Names

While naming your variables in JavaScript keep following rules in mind You should not use any of the JavaScript reserved keywords as a variable name. These keywords are mentioned in the next section. For example, break or Boolean variable names are not valid JavaScript variable names should not start with a numeral (0-9) They must begin with a letter or the underscore character. For example, the 123test is an invalid variable name but the 123test is a valid one. JavaScript variable names are case sensitive. For example, Name and name are two different variables  

JavaScript Reserved Words

The following are reserved words in JavaScript. They cannot be used as JavaScript variables, functions, methods, loop labels, or any object names.  

abstract Boolean break byte case catch char const continue debugger default delete do double  else enum export extends false final finally float for function goto if implements import in  instance of int interface long native new null package private protected public return short static superswitch synchronized this throw throws transient true try typeof var void volatile while with