- JavaScript Array provides lot of useful methods to working with.
- In this Demo, “We will explore all the methods present in JavaScript Array With Example“.
- Check the below code for all the Array Method Declaration using function:-
var ARRAY_METHOD={
/*Concat : Combines two array*/
getConcatResult:function(array1,array2){
return array1.concat(array2);
},
/*Shift : Removes the First Element of the array*/
getShiftResult:function(array){
return array.shift();
},
/*UnShift : Adds new elements at the begining of an array and returns the new length*/
getUnshiftResult:function(array){
return array.unshift('vegetable','snack');
},
/*Reverse : Reverse the Array Elements*/
getReverseResult:function(array){
return array.reverse();
},
/*Push : Add the new element at the rear position of an array*/
getPushResult:function(array){
return array.push("dinner");
},
/*Join : concat array elements to a string using the input token*/
getJoinResult:function(array){
return array.join("---##**##---");
},
/*Slice : Return a new Array object with selected elements using input index and length*/
getSliceResult:function(array){
return array.slice(1,4);
},
/*Splice : Adds or removes the element to an array specified by the index */
getSpliceResult:function(array){
return array.splice(1,2,'new1','new2');
},
/*ValueOf : Returns the String representation of an array*/
getValueOfResult:function(array){
return array.valueOf();
},
/*LastIndexOf : Retruns the last matched position of an element in the array*/
getLastIndexOfResult:function(array,searchCharacter){
return array.lastIndexOf(searchCharacter);
},
/*Pop: Removes the last element in the array*/
getPopResult : function(array){
return array.pop();
},
/*Pop: Removes the last element in the array*/
getIndexOfResult : function(array){
return array.indexOf('vegetable');
}
};
- The html file where we are calling these wrapped methods are as below:-
<!DOCTYPE html>
<html>
<head>
<title>Javascript Array Methods</title>
</head>
<body>
<script src="js-array-script.js"></script>
<script>
var arr1 = ['Apple','Mango','Grapes'],
arr2 = ['Rose','Lotus','Jasmine','lily'],
output;
output = ARRAY_METHOD.getConcatResult(arr1,arr2);
console.log("AFTER CONCAT :-");
console.log(output);
output = ARRAY_METHOD.getShiftResult(arr1);
console.log("SHIFTED ELEMENT IS:-");
console.log(output);
output = ARRAY_METHOD.getUnshiftResult(arr1);
console.log("AFTER UNSHIFT :-");
console.log(output);
output = ARRAY_METHOD.getReverseResult(arr1);
console.log("AFTER REVERSE :-");
console.log(output);
output = ARRAY_METHOD.getPushResult(arr1);
console.log("AFTER PUSH LENGTH :-");
console.log(output);
output = ARRAY_METHOD.getJoinResult(arr1);
console.log("AFTER JOIN :-");
console.log(output);
output = ARRAY_METHOD.getSliceResult(arr1);
console.log("AFTER SLICE :-");
console.log(output);
output = ARRAY_METHOD.getSpliceResult(arr1);
console.log("AFTER SPLICE :-");
console.log(output);
output = ARRAY_METHOD.getValueOfResult(arr1);
console.log("AFTER VALUEOF :-");
console.log(output);
output = ARRAY_METHOD.getSliceResult(arr1);
console.log("AFTER SLICE :-");
console.log(output);
output = ARRAY_METHOD.getLastIndexOfResult(arr1,"vegetable");
console.log("AFTER LAST INDEX OF :-");
console.log(output);
output = ARRAY_METHOD.getIndexOfResult(arr1);
console.log("AFTER INDEX OF :-");
console.log(output);
output = ARRAY_METHOD.getPopResult(arr1);
console.log("POPPED ELEMENT :-");
console.log(output);
</script>
</body>
</html>
- The output or log in console will look like below:-