Professional Documents
Culture Documents
CSS Lec-11 Methods of Array.1bf5f3a
CSS Lec-11 Methods of Array.1bf5f3a
ii) pop() : This method removes the last element from an array and
returns the value that was removed.
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
a.unshift(100,200);
console.log(a);
Output: [100,200,10,20,30,40,50]
a.shift();
console.log(a);
Output: [200,10,20,30,40,50]
Because of this, the sort() method will produce incorrect result when
sorting numbers. You can fix this by providing a compare function
function(a, b) {return a - b}
When the sort() function compares two values, it sends the values to the
compare function, and sorts the values according to the returned
(negative, zero, positive) value.
i) If the result is negative a is sorted before b.
ii) If the result is positive b is sorted before a.
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
iii) If the result is 0 no changes are done with the sort order of the two
values.
Example
var points = [40, 100, 1, 5, 25, 10];
points.sort(function(a, b){return a - b});
console.log(points);
Output : [ 1,5,10,25,40,100]
Output: 40,100,1,5,25,10
The join() method also joins all array elements into a string. It behaves
just like toString(), but in addition you can specify the separator.
Example
var points = [40, 100, 1, 5, 25, 10]
console.log(points.join(' # '));
Output: 40#100#1#5#25#10
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
Note: if end occurs before start , no elements are copied to the new
array.
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
The first parameter (2) defines the position where new elements should
be added (spliced in).
Example:
var t = fruits.splice(1,2,"Lemon","Kiwi");
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Client Side Scripting, Prepared By: Atul Kabra, 9422279260
Example
console.log(fruits.indexOf("Banana"));
Output : 0
console.log(fruits.lastIndexOf("Banana"));
Output : 2
console.log(fruits.indexOf("Apple"));
Output : -1
Output: [100,30,20,10]
Course: Java Script, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260