Different Type of Variables Available in JavaScript
Variables are used to store values. Variable names can have uppercase letters (A-Z), lowercase letters (a-z), digits (0-9), underscore ( _). The first character should be an alphabet or an underscore. Variables cannot have blank spaces or other punctuation marks. Variables are case sensitive. Variables names can be of any length. Variables are defined with the optional keyword var. Unlike other programming languages, JavaScript does not require you to specify the type of data contained in a variable. The same variable can hold different values such as String, Integer, Boolean, etc.,
Example: Java
Script2
-Style
The following example illustrates how to declare a variable and assign a value to it.
1 2 3 4 5 6 7 8 9 10 11 12 | <html> <head> <title> My first JavaScript program</title> <Script language=”JavaScript”> <!- Java=4 document.writeIn(“Welcome to JavaScript”) document.write(Java) //-> </script> </head> </html> |
Variables
- Number
- Boolean
- String
- Null
- Undefined.
Number types
The number is of two types namely :
- Integer
- Floating Point
Integer literals can be represented in three different bases :
- A decimal integer which consists of digits from 0 through 9 [ Example 25]
- A hexadecimal integer begins with Ox or OX [Example 0x20, 0X55]
- An octal number begins with 0. [Example 033]
Floating-point literals are used to represent numbers with decimal point or numbers which require exponentiation. The following example illustrates how to declare decimal, hexadecimal and octal numbers.
Code –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <html> <head> <title> My first JavaScript program </title> <script language = “JavaScript”> < !- Java = 4 A = 0xa B = 0xb C = A+B D = 2e2 F = Java +D document.write(“Welcome to JavaScriptStyle”) document.write(“<br>”) document.write(“Java = “ + Java) document.write(“<br>”) document.write(“A+B=” +C) document.write(“<br>”) document.write(“ Java +D = “ +F) //-> </script> </head> </html> |
String Values
String values can be given within single or double quotes. To include quotes or double quotes within strings special formatting characters have to be used. The following are some of the formatting characters :
Character | To be used for |
\’ | single quote |
\” | double quote |
\\ | backslash |
\t | horizontal tab |
\n | newline |
\b | backspace |
\r | carriage return |
\f | form feed |
Code–
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <html> <head> <title> My first JavaScript program </title> <pre> <script language =”JavaScript”> <!- document.write(“Welcome to JavaScript”) document.write(“This is \t your first session”) document.write(“You would be learning \n about JavaScript”) //-> </script> </pre> </head> </html> |
Arrays
The array is a special type of JavaScript object. Arrays are used for storing data in a contiguous manner. For example, if you want to store the names of courses taught at JavaScriptStyle, you can use arrays. The syntax for declaring an Array is :
Course = new Array(3)
To assign values to an array the following statements can be used :
1 2 3 | Course[0] = “E-commerce” Course[1] = “AIPP” Course[2] = “Visa Banking” |
The following example illustrates how to declare an array and to write array elements to the document.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <html> <head> <title> Declaring arrays <title> <script language = “JavaScript”> <!- Course = new Array(3) Course[0] = “E-commerce” Course[1] = “AIPP” Course[2] = “Visa Banking” document.write(“Courses taught at JavaScriptStyle” + “<br>”) document.writeIn(“First Element:” +course[0]+”<br>”) document.writeIn(“Second Element:” +course[1]+”<br>”) document.writeIn(“Third Element:” +course[2]+”<br>”) //-> </script> </head> </html> |
The array index begins at 0. So to declare an array for storing 5 elements it is enough to write new Array(4).
An array should be declared before it is used in the script. The length of an array is the number of elements it can hold. To declare an array which should have dynamic length the following syntax is used :
Courses = new Array ()
When any reference is made to that array like courses[5]=” Java” then the array length dynamically increases to 6. The following example illustrates the use of the dynamic array :
Code->
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <html> <head> <title>Dynamic Array</title> <script language=”JavaScript” <!- Course=new Array() Course[5]=”Java” document.write(“Courses taught at JavaScript Style”+”<br>”) document.writeIn(course[5]+”<br>”) document.writeIn(“The length of the array is “+”<br>”) document.writeIn(course.length) //-> </script> </head> </html> |
Dense Arrays :
A dense array is one in which the array elements are assigned initially while declaring. They are declared by listing the values instead of the array length. The syntax for declaring a dense array is as follows :
Array Name=new Array(element0, element1, ……,element n )
The following example illustrates the use of a dense array:
Code–
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <html> <head> <title> DenseArrays </title> <script language=”JavaScript”> <!- Course=new Array(“E-commerce”, “AIPP”, “Visa Banking”) document.write(“Courses taught at JavaScript Style”+<br>”) document.writeIn(course[0]+”<br>”) document.writeIn(course[1]+”<br>”) document.writeIn(course[2]+”<br>”) //-> </script> </head> </html> |