Page 204 - Web Applications (803) Class 12
P. 204
There are several methods available for arrays and strings:
Method Description Example
Name
concat() Joins two strings result = “Harry”.concat(“Potter”);
document.write(result);
Output: HarryPotter
join() Converts an array into a string const animals = [“lion”, “tiger”, “cat”, “monkey”];
var text1 = animals.join();
document.write(text1+”<br>”);
var text2=animals.join(“ and “);
document.write(text2);
Output:
lion,tiger,cat,monkey
lion and tiger and cat and monkey
pop() Removes the last element in an array const languages = [“R”, “C”, “C++”, “Basic”];
document.write(languages.pop());
Output: Basic
push() Adds the new element at the end in an const num = [10,20,30,40,50];
array num.push(92);
for(i=0;i<num.length;i++)
document.write(num[i]+”,”);
Output: 10,20,30,40,50,92,
reverse() The order of the elements in an array is const fruits = [“pear”, “pinapple”, “guava”, “cherry”,
reversed via the reverse() method. The “olive”];
original array is overwritten. document.write(fruits.reverse());
Output: olive,cherry,guava,pinapple,pear
shift() Returns the first element of an array const fruits = [“pear”, “pinapple”, “guava”];
after removing it document.write(fruits.shift());
Output: pear
slice() Selected array elements are returned as const fruits = [“pear”, “pinapple”, “guava”, “cherry”,
a new array. The method begins from a “olive”];
specified start and selects elements till const list1 = fruits.slice(1, 3);
end-1 position. document.write(list1);
The original array remains unchanged. Output: pinapple,guava
202 Touchpad Web Applications-XII

