Understanding Scripting (part 2)

Understanding Scripting

Defining Variables

Consider a situation where you have created a Web page that accepts the quantity and unit price of  a product that users wishes to purchase and displays the total price of the product. While reading the quantity provided by the user , you need to store this value so that it can be used to calculate the total price. In addition you need to store the price of the product. 

A variable is a named location in memory that is used to store a value. In the preceding example , if the user provides 5 as the quantity of a product that needs to be purchased and the price of the product is $250, you need two variables, one for storing the quantity and other for the price. 

The following code snippet shows the assignment of values to  variables:

quantity= 5
price= 250


Declaring a Variable

Before using a variable in your program, you should declare it first. JavaScript allows you to declare a variable by using the var keyword. The syntax to declare a variable is:

                                                                   var var_name;

In the preceding syntax, var_name refers to the name of the variable and the variable is declared by using the var keyword.

The following code snippet declared a variable named employeeName :
                                    
                                                                 var employeeName;

JavaScript which is loosely-typed language , allows you to initialize a variable without specifying its data type. Numeric, String and Boolean are commonly used data-types in JavaScript.

Assigning a Value to a Variable

Values can be assigned to a variable in the following ways:
  • Assigning a value to a variable after its declaration.
  • Initializing a variable while declaring it.
  • Initializing a variable without declaring it explicitly.


                                               Assigning a Value To a Variable After Its Declaration

Consider the following code snippet to initialize a variable  after its declaration:

                                                             var employeeName;
                                                            employeeName="Peter"

The preceding code snippet declares the variable , employeeName and then assign the value Peters to it.

Initializing a Variable While Declaring It

Consider the following code snippet to initialize a variable while declaring it:

                                                 var   employeeName="Peter"

 The preceding code snippet declares the variable , employeeName and assign its value Peter.

Initializing a Variable Without Declaring It Explicitly

JavaScript provides you the flexibility to use a variable without declaring it. When a variable is used in a script without declaring it explicitly, it is automatically declared when used for the first time.However, it is a good practice to declare a variable before using it in a script as it increases the readability of a program. 

Consider the following code snippet to initialize a variable without declaring it:
                                                           employeeName="Peter"

In the preceding code snippet, the variable, employeeName, is assigned a value but not declared explicitly. Therefore, the statement,  employeeName="Peter" , declares the variable, employeeName, implicitly and assign the value Peter to it.


Note: An array is a special type of variable that is used to store multiple values arranged in a definite sequence. These values are stored in indexed locations within the array. 
Before using arrays, you need to declare them. 

    The following syntax is used for declaring  an array:

                                            var category = new Array(3)

In the preceding syntax, an array named category is created with a size of three.

Consider the following code snippet to assign a value at indexed location

                                                category [0] = "Soap"
                                                category [1] =  "Oils"
                                                category [2] =  "Food Products"

The value stored in the array can be accessed by using the index location where they are stored. The following syntax is used for accessing the first element in the array :           
                                                      category [0] ;




About Codingwelove

Codingwelove
Recommended Posts × +

0 comments:

Post a Comment