Skip to main content

One of my friend use [].slice.call()friends uses [].slice.call() to fill an array with match elementmatched elements. 

I wonder how itsthis works. I just know the .push methodArray.prototype.push to fill an array. 

Also I have seen there is a difference in both techniquetechniques to fill an array. 

So my question is how .slice help to fill an array and what is its role and .push method make and object and .slice make direct array. questions are:

  • How does Array.prototype.slice help to fill an array?
  • What are the roles of Array.prototype.slice and Array.prototype.push to make objects and arrays?

fiddleFiddle

var arr=[];
var arr=[].slice.call($('[id^=name]'));
//arr.push($('[id^=name]'))
console.log(arr)

One of my friend use [].slice.call() to fill an array with match element. I wonder how its works. I just know .push method to fill an array. Also I seen there is difference in both technique to fill an array. So my question is how .slice help to fill an array and what is its role and .push method make and object and .slice make direct array. fiddle

var arr=[];
var arr=[].slice.call($('[id^=name]'));
//arr.push($('[id^=name]'))
console.log(arr)

One of my friends uses [].slice.call() to fill an array with matched elements. 

I wonder how this works. I just know the Array.prototype.push to fill an array. 

Also I have seen there is a difference in both techniques to fill an array. 

So my questions are:

  • How does Array.prototype.slice help to fill an array?
  • What are the roles of Array.prototype.slice and Array.prototype.push to make objects and arrays?

Fiddle

var arr=[];
var arr=[].slice.call($('[id^=name]'));
//arr.push($('[id^=name]'))
console.log(arr)
Source Link
Jitender
  • 8k
  • 32
  • 117
  • 218

.slice.call vs .push in javascript

One of my friend use [].slice.call() to fill an array with match element. I wonder how its works. I just know .push method to fill an array. Also I seen there is difference in both technique to fill an array. So my question is how .slice help to fill an array and what is its role and .push method make and object and .slice make direct array. fiddle

var arr=[];
var arr=[].slice.call($('[id^=name]'));
//arr.push($('[id^=name]'))
console.log(arr)