• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

JavaScript Variables & Data Types

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. 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 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 you 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 JavaScript from an external file source, you need to wirte your all JavaScript source code in a simple text file with extension “.js” and then include that file as shown above.

For example, you can keep following content in filename.js file and then you can use 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 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 global scope which means it is defined every where in your JavaScript code.

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.

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 keyword as 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, 123test is an invalid variable name but 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

super

switch

synchronized

this

throw

throws

transient

true

try

typeof

var

void

volatile

while

with

 

 

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on JavaScript Variables & Data Types"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.