Page 270 - Web_Application_v2.0_C12_Fb
P. 270

const fruits = ["pear", "pineapple", "guava", "cherry", "olive"]; document.write(fruits.
                      reverse());
                      Output: olive,cherry,guava,pineapple,pear
                  4.  Explain the unshift() method in JavaScript.
                 Ans.  The unshift( ) method inserts new elements at the beginning of an array. The original array is overwritten.
                     For example:

                    const col1 = ["blue","green","orange","purple"];
                    col1.unshift("silver", "golden");
                    document.write(col1);
                    Output: silver,golden,blue,green,orange,purple.
                  5.  Write the differences between substring() and slice() functions in JavaScript.
                Ans.    substring(): extracts characters between two indices.

                      var str = "JavaScript";
                      var result = str.substring(4, 10);
                      document.write(result);
                      Output:
                      Script
                      slice(): extracts a part of a string and returns it as a new string.

                      var str = "Hello World";
                      var result = str.slice(0, 5);
                      document.write(result);
                      Output:
                      Hello

                  6.  How can you determine the number of characters in a string?
                 Ans.  You can determine the number of characters in a string using the .length property in JavaScript.
                     For example:

                    var text = "Hello, World!";
                    document.writetext.length);
                     // Output: 13
                     The .length property returns the total count of characters, including spaces and special symbols.
                 7.  What will be the Output of the following code:                                    [CBSE Handbook]
                    <!DOCTYPE HTML>
                    <HTML>
                    <BODY>
                    <H2>JavaScript Functions</H2>
                    <P ID="demo1"></P>
                    <P ID="demo2"></P>
                    <SCRIPT>
                    var x=170
                    function myFunction()
                    {
                    var x=45
                    document.getElementById("demo1").innerHTML = x

                268   Touchpad Web Applications (Ver. 2.0)-XII
   265   266   267   268   269   270   271   272   273   274   275