jQuery's .eq() is:
eq: function( i ) {
return i === -1 ?
this.slice( i ) :
this.slice( i, +i + 1 );
},
What is the point of the first + in +i + 1?
It's to cast the value to integer and to ensure that you are performing an integer addition instead of string concatenation.
Compare those two for example:
var i = '1';
var result = i + 1; // result = '11';
var result2 = +i + 1; // result = 2;
And to answer the question why this is not used as the first argument of the slice method, it is because the slice method internally performs the conversion. So for example the following will work as expected:
var array = [1, 2, 3];
var result = array.slice('1', '2'); // result = [2];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With