Javascript1

# What is data type & Variable?

### Number?

The number type deals with digits, It covers both floating point numbers and integers.

Floating point numbers are like - 412.562, 9.2, 0.468

Integer numbers are like : 25, 1258, 968

Initializing number type variables

Var age = 25
Var is javascript keyword, age is a variable name, 25 is value assigned to age variable, variable age is of number type because the value assigned to age is of number type.

Var hra = 1585.56
Var is javascript keyword, hra is a variable name, 1585.56 is value assigned to hra variable, variable hra is of number type because the value assigned to hra is of number type.

In the example given below we have used keyword document.write (messge ) which is used to print the message or variable value given with it.

Example

<html>
<body>
<script type="text/javascript">

var age=25 document.write("Age is : "+age);

</script >
</body>
</html>

Understanding the program

<script > tag is the start of a javascript program, var is to declare a variable of name age and 25 is the value assigned to the variable age. document.write is used to print the given message and/or value of a given variable. We have used + sign here which is used to concatenate the message ( Age is : ) and the value of variable a (25).

Output of this program

 Age is : 25

Example

<html>
<body>
<script type="text/javascript">

var hra=1585.25 document.write("HRA is : "+hra);

</script >
</body>
</html>

Understanding the program

<script > tag is the start of a javascript program, var is to declare a variable of name hra and 1585.25 is the value assigned to the variable hra. document.write is used to print the given message and/or value of a given variable. We have used + sign here which is used to concatenate the message ( HRA is : ) and the value of variable a (1585.25).

Output of this program

 HRA is : 1585.25

Click hra to view this program in browser
[Top]

### String?

String type deals with a single character or a series of characters enclosed with single quotes or double quotes respectively.

Initializing string type variables

var sect = ‘a’
var is JavaScript keyword, sect is a variable name, a is value assigned to sect variable, variable sect is of string type because the value assigned to sect is of string type.

var country=”India”
var is JavaScript keyword, country is a variable name, India is value assigned to country variable, variable country is of string type because the value assigned to country is of string type.

In the example given below we have used keyword document.write (message ) which is used to print the message or variable value given with it.

Example 1

<html>
<body>
<script type="text/JavaScript">

var sect=’a’
document.write("Section is : "+sect);

</script >
</body>
</html>

Understanding the program

<script > tag is the start of a JavaScript program, var is to declare a variable of name sect and ‘a’ is the value assigned to the variable sect. document.write is used to print the given message and/or value of a given variable. We have used + sign here which is used to concatenate the message ( Section is : ) and the value of variable sect (a).

Output of this program

 section is : a

Example 2:

<html>
<body>
<script type="text/JavaScript">

var country="India"
document.write("Country is : "+country);

</script >
</body>
</html>

Understanding the program

<script > tag is the start of a JavaScript program, var is to declare a variable of name country and “India” is the value assigned to the variable country. document.write is used to print the given message and/or value of a given variable. We have used + sign here which is used to concatenate the message ( Country is : ) and the value of variable country (India).

Output of this program

 Country is : India

Click string to run this program in browser
[Top]

### Boolean?

Boolean type has only two values true and false. These constant values are case-sensitive.

Initializing Boolean type variables

var present = true

var is JavaScript keyword, present is a variable name, true is value assigned to present variable, variable present is of Boolean type because the value assigned to present is of boolean type.

In the example given below we have used keyword document.write (message ) which is used to print the message or variable value given with it.

Example 1

<html>
<body>
<script type="text/javascript">

var student = true
var attend = false
document.write("Student Status in School : "+present);
document.write("<br>Class Attended : "+attend);

</script >
</body>
</html>

Understanding the program

present & absent are two variables of Boolean type having values true and false respectively. <BR> is a html tag used to insert a new line.

Output of this program

 Student Status in School : true Class Attended : false

Click boolean to view results in browser
[Top]

### Null & undefined?

Null type has only one value, null. The null value means ‘no data’ or this variable do not have any useful data.

Undefined type has one value, undefined, undefined means nothing is stored in this variable. Undefined is not even null.

Initializing null type variable

var food = null

var is JavaScript keyword, food is a variable name, null is value assigned to food variable.

In the example given below we have used keyword document.write (message ) which is used to print the message or variable value given with it.

Example 1

<html>
<body>
<script type="text/JavaScript">

var food=null
document.write("food is : "+food);

</script >
</body>
</html>

Understanding the program

food is a variable of null type having no type of values or data.

output of this program

 food is : null

Knowing undefined type variable
var food ;
var is JavaScript keyword, food is a variable name, no value is assigned to variable food.

In the example given below we have used keyword document.write (message ) which is used to print the message or variable value given with it.

Example 2

<html>
<body>
<script type="text/javascript">

var food;
document.write("food is : "+food);

</script >
</body>
</html>

Understanding the program

food is a variable of undefined type , because nothing is defined to it .

Output of this program

 food is : undefined

click null to view result of both above programs(combined) in browser
[Top]

### infinity and NAN?

infinity is a property of a number. It represents mathematical infinity.

Example

Var infi= 1e400*1e350;
Var is javascript keyword, infi is a variable name, 1e400 * 1e350 value assigned to infi variable.

In the example given below we have used keyword document.write (message ) which is used to print the message or variable value given with it.

Example 1

<html>
<body>
<script type="text/javascript">

var infi = 1e300 * 1e300;
document.write("Value stored in infi is : "+infi);

</script >
</body>
</html>

Output of this program

 Value stored in infi is : Infinity

Nan Stands for not a number and is a result of a mathematical operation which do not have any sense. This happens generally when we divide a 0 by 0.

Example

var k = 0/0 ;
var is JavaScript keyword, k is a variable name, 0/0 value is assigned to variable k.

In the example given below we have used keyword document.write (message ) which is used to print the message or variable value given with it.

Example 2

<html>
<body>
<script type="text/JavaScript">

var k = 0/0;
document.write("Value stored in k is : "+k);

</script >
</body>
</html>

Output of this program

 Value stored in k is : NaN

Click NaN to view result of both the above programs(combined) in browser
[Top]

### Arrays?

If we require many variables of same or different types then a problem of remembering names of variables and many functional problems will arise. Concept of array allows us to store different data type data pieces under the same name in an ordered way. It helps in creating many variables. There is no need of remembering their names because they all have the same name but different positions in the array. The count of location in an array starts from 0 not from 1, means the first location is 0th location and 12th is 11th .

Declaring an array variable

Method 1
var d = new Array (“One”, “Two”, “Three”, “Four”);

Understanding the declaration

var - is reserve word

d - is the name of the array

new - is a reserve word , sets that much number of locations in memory as much parameters given with Array( );

Array( ) - is a reserve word , provides the input to new that how much memory locations are to be reserved.

“One”, “Two”, “Three”,” Four” – are the values to set in d named array variable as much the count of these variables is that much of locations in memory will be reserved by new (reserve word).

Method 2
var sales = new Array (12);

Understanding the declaration

var - is reserve word

sales - is the name of the array

new - is a reserve word , sets that much number of locations in memory as much parameters given with Array( ), in this case 12 locations will be occupied;

Array( ) - is a reserve word , provides the input to new that how much memory locations are to be reserved. 12 – is the count of locations to be reserved.

Assigning values to an array locations

Sales = “Rubber”;

Sales  = 12000;

Sales = “Plastic”;

Sales  = 18000;

At 0th position of sales named array the value is “Rubber “ and at 1st position 12000 is stored. So we can place different data types data in the same array.

Example 1

<html>
<body>
<script type="text/JavaScript">

var d = new Array("One","Two","Three","Four");
document.write("Value at 0th position of d is "+d);
document.write("<br>Value at 1st position of d is "+d);
document.write("<br>Value at 2nd position of d is "+d);
document.write("<br>Value at 3rd position of d is "+d);

</script >
</body>
</html>

Output is

 Value at 0th position of d is One Value at 1st position of d is Two Value at 2nd position of d is Three Value at 3rd position of d is Four

Example 2

<html>
<body>
<script type="text/javascript">

var sales = new Array(12);
sales="Rubber";
sales=12000;1
sales="Plastic"
sales=18000;
document.write("Value at 0th position of sales is "+sales);
document.write("<br>Value at 1st position of sales is "+sales);

</script >
</body>
</html>

Output is

 Value at 0th position of sales is Rubber Value at 1st position of sales is 12000

Click array to see result of above programme in browser
[Top]
«<Javascript0|Javascript2»>

page revision: 19, last edited: 22 Jun 2007 12:31