Navigation Menu

Approach to Javascript Programming



JavaScript is a client side scripting language which gives the functionality of the application.

JavaScript code is executed by Web Browser which is the only language understood by web browsers.

JavaScript is run by a JavaScript engine which is a part of web browser.
So, the development environment for JavaScript is Notepad and the run time environment is Web Browser.

In Notepad, the scripting code is either written in <head> section or <body> section.

JavaScript code is inserted within <script>...</script> tags.

Syntax: <script>

                     // JavaScript code

            </script>

Example:




Creating Variables

JavaScript does not have a concept of typed variables. JavaScript variables are declared by using var keyword.

Syntax: var variable_name;

Accessing Html Elements Through JavaScript

JavaScript uses object document to traverse html elements. The method getElementById() is used to access elements of html by thier ID.

Example:




Conditional Statements

1. If() statement:

   Syntax:   if(<condition>)
                {
                             // block of statemnts.
                }




2. If...else() statement:

   Syntax:  if(<condition>)
                {
                               // block of statements.
                }
                else
                {
                      //block of statements.
                }


3. Switch() statement:

   Syntax:   switch (expression)   {
                   case n:
                         code block
                         break;

                  case n:
                         code block
                         break;

                  default:
                        default code block
}   



Looping Statements 


1. for() loop:

   Syntax:  for(initialisation;condition;increment/decrement)
                {
                  //block of statements
                }






2. while() loop:

   Syntax:  while(condition)
               {
                  //block of statements
               }



JavaScript Functions

Function is a block of statements which executes whenever called and performs some task.


Function Declaration

JavaScript functions are declared using function keyword.

syntax:  function(arguments)
             {
                   // statements
             }



Anonymous Functions

The function above is actually an anonymous function (a function without a name).
Functions stored in variables do not need function names. They are always invoked (called) using the variable name.




JavaScript Objects

Objects are variables which contain variables. Objects are used to store collection of values.


The values are stored in name : value pair.

Name represents property and value represents property value.

Syntax:  var variable_name = { property1 : “value1”, property2 : “value2” }



























0 comments: