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

splice()       The  splice()  method  adds  and/or  const fruits = [“pear”, “pinapple”, “guava”, “cherry”,
                                  deletes elements from an array. The  “olive”];
                                  original array is replaced.            // At position 3, insert 2 elements:
                                                                         fruits.splice(3, 0, “papaya”, “melon”);
                                                                         document.write(fruits);
                                                                         // At position 2, remove 2 items:

                                                                         fruits.splice(2, 2);
                                                                         document.write(“<br>”+”After removing elements
                                                                         “+fruits);
                                                                         Output:
                                                                         pear,pinapple,melon,cherry,olive

                   unshift()      Inserts new elements at the beginning  const characters = [“Harry”, “Ron”, “Hermione”,
                                  of an  array. The original  array  is  “Neville”];
                                  overwritten.                           characters.unshift(“Hagrid”, “Dudley”);
                                                                         document.write(characters);
                                                                         Output:

                                                                         Hagrid,Dudley,Harry,Ron,Hermione,Neville

                   indexOf()      Returns  the  location  of  a  value’s  first  var text = “The cheetah landed in India on 17
                                  appearance in a string.                September 2022”;
                                                                         document.write(text.indexOf(“in”));
                                                                         Output: 19

                   match()        Finds a match within a string          var text = “The rain in INDIA arrives every year
                                                                         in June”;

                                                                         document.write(text.match(“ear”));
                                                                         document.write(“<br>”+text.match(“india”));
                                                                         Output:
                                                                         ear
                                                                         null

                 Creating a JavaScript Object

                 You can define and create your own objects in any of the following ways:
                   Using an object literal.
                 The following example creates a new JavaScript object with three properties:

                    const student = {Name : "Uma", age : 15, Address  : "Green Park" };
                                                                   OR
                 Create an empty JavaScript object, and then add the properties using dot notation as shown:

                    const student = {};
                    student.Name = "Uma";
                    student.age = 15;
                    student.Address = "Green Park";
                                                                                          Web Scripting—JavaScript   203
   200   201   202   203   204   205   206   207   208   209   210