Page 146 - DCAP408_WEB_PROGRAMMING
P. 146

Web Programming




                    Notes          A good rule of thumb is to have your variable names start with a lowercase letter (a-z) and use
                                   underscores to separate a name with multiple words (i.e. my_var, strong_man, happy_coder,
                                   etc).

                                   5.3.3 Variable  Scope

                                   Variable scope has to do with where a variable can legally be used, and is determined by where
                                   it was originally declared or initialized.
                                   A variable declared or initialized outside a function body has a global scope, making it accessible
                                   to all other statements within the same document.
                                   A variable declared or initialized within a function body has a local scope, making it accessible
                                   only to statements within the same function body.

                                   Self Assessment

                                   Fill in the blanks:
                                   8.  A ……………….. is a symbolic name that represents some data that you set.
                                   9.  To create a variable, the ……………….. keyword precedes the variable name, and is used
                                       only once for declaration.

                                   5.4 Arrays

                                   An array is a special variable, which can hold more than one value, at a time.
                                   If you have a list of items (a list of car names, for example), storing the cars in single variables
                                   could look like this:
                                   cars1=”mecedes”;
                                   cars2=”ferari”;

                                   cars3=”BMW”;
                                   However, what if you want to loop through the cars and find a specific one? And what if you had
                                   not 3 cars, but 300?

                                   The best solution here is to use an array!
                                   An array can hold all your variable values under a single name. And you can access the values
                                   by referring to the array name.




                                     Did u know?  What are the advantages of Arrays in all the programming languages?

                                   Advantages

                                   1.  You can use one name for similar objects and save then with the same name but different
                                       indexes.
                                   2.  Arrays are very useful when you are working with sequences of the same kind of data
                                       (similar to the first point but has a different meaning).
                                   3.  Arrays use reference type and so.





          140                               LOVELY PROFESSIONAL UNIVERSITY
   141   142   143   144   145   146   147   148   149   150   151