Page 191 - Web Applications (803) Class 12
P. 191

JavaScript differs from other languages like Java as there is no scope for blocks. However, functions have scope.
                 Therefore, the entire function will be able to see a variable that is defined using the var keyword in a compound
                 expression (such as inside an if control structure).


                                                 Lab Assignment ‘n Activity                             Interdisciplinary



                    Write JavaScript code for the following:
                      Initialise variables for principal, rate and time. Calculate and display simple interest.

                       Initialise a string variable with your name. Find the length of this string. Then display the square of
                       that number.
                       Initialise  variable for length, breadth and height of a cuboid. Calculate and display the
                       volume of a cuboid. (Hint: Volume = length*breadth*height)
                       Initialise variable for radius of a circle. Calculate and display the area and circumference of
                       the circle.




                   3.7 CONTROL STRUCTURES

                 If and else, as detailed below, support conditional statements.
                 •  If the stated condition is true, specify a block of code to be executed; otherwise, specify a block of code to
                    be performed if the same condition is false.
                 •  If the first condition is false, if (statement) specifies a new condition to test. (else if….)

                 •  The switch (statement) specifies a number of different code blocks to be performed.




















                 Let us now understand the working of the if….else statements in JavaScript.
                 Syntax:

                    if (condition) {
                      //  block of code to be executed when the condition is true
                    } else {
                      //  block of code to be executed when the condition is false
                    }






                                                                                          Web Scripting—JavaScript   189
   186   187   188   189   190   191   192   193   194   195   196