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

Ans.  i.  var dt=new Date();         // The date and time are stored in the built-in
                                                    //JavaScript Date object.
                       document.write(dt);
                     ii.  var str1 = "Humpty Dumpty sat on a wall";

                      var pos = str1.search("sat");
                      document.write(pos);
                     iii. const col = [“red”, “green”, “blue”]

                        a.  col.push("silver");
                        b. const fruits = ["apple","orange"]
                          const new_arr = col.concat(fruits);
                        c.  col.sort();

                        d. col.reverse();
                  9.  What are the important properties of an anonymous function in JavaScript? Give an example.     [CBSE T2 2022]
               Ans.  A function that has no name attached to it is called an anonymous function. To define a function in JavaScript, we
                    typically use the function keyword followed by the function name; however, for anonymous functions, we only
                    use the function keyword.
                     An anonymous function is not accessible once it is created; rather, a variable is used to store the function as a
                    value. Example:

                    <script>
                    var work = function () {
                      document.write("London Bridge is falling down!");

                    };
                    work();
                    </script>
                     An anonymous function may be more syntactically straightforward than using a named function if it is only called
                    once or occasionally.
                     It is perfect for handling rapid customization of DOM elements and single-line event handlers.
                 10.  Explain “Fall through” in a switch statement with the help of an example.
               Ans.  JavaScript supports the fall-through behaviour in switch. If a case statement does not have a “break” statement,
                    the code for the subsequent cases is executed until a break statement is found. Hence, the “break” statement is
                    essential when using the switch statement.
                     If the “break” statement is not given, all the cases after the "correct" case are executed until the last case, including
                    the default case. Example:

                    var num = 8;
                    switch(num){
                        case 2:

                        case 4:
                        case 8: document.write("Even Number"); break;
                        case 1:
                        case 3: document.write("Odd Number"); break;
                        default: document.write("Invalid");

                224   Touchpad Web Applications-XII
   221   222   223   224   225   226   227   228   229   230   231