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

Variables in JavaScript

The following example illustrates how to declare a variable and assign a value to it.

Variables

  1. Number
  2. Boolean
  3. String
  4. Null
  5. Undefined.

VariablesNumber  types

The number is of two types namely :

  1. Integer
  2. Floating Point

Integer literals can be represented in three different bases :

  1. A decimal integer which consists of digits from 0 through 9 [ Example 25]
  2. A hexadecimal integer begins with Ox or OX [Example 0x20, 0X55]
  3. 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 –

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
\rcarriage return
\f form feed

Code

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 :

The following example illustrates how to declare an array and to write array elements to the document.

 

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->

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