Wednesday 9 November 2016

Variables in Java Script

Like other programming languages ,Java Script also supports the variables. Variable is simply a place holder for a value.It can also be called as the container where we can place our values.Thus in order to store any value ,we need to register a space in the memory  that is to declare a variable and then initializing it with some value. It is necessary to declare a variable before using it.
Variables are declared in Java Script with keyword var is as follows:

<html>
<head><title>Java Script Variables</title>

<script type="text/javascript">
var a;
var b;
</script>

</head>
</html>
The process of assigning values to a variables is called as initialization process.Variables can be initialized at any point of time in program before using it.It can also be initialized  at the time of creation of variables.
Now we assign 10 to variable a and value 20 to variable b as follows:

<script type="text/javascript">
var a=10;
var b=20;
</script>

In Java Script it is not required to specify the data type for  a variable .A variable declared with Keyword 'var ' accepts all the values whether it is a character or decimal point or integer value.
Thats  why Java Script is also called as Untyped language.

In Java Script variables are of two types:
Local Variables and Global Variables

The variables which are declared with in the function block is called local variables and the scope of these variables lies only with in the function block.

While the variables declared outside the bock of function are called Global variables.These variables can be accessed anywhere with in the program.

No comments:

Post a Comment