Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding jQuery's .eq()

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?

like image 681
Randomblue Avatar asked Jan 23 '26 07:01

Randomblue


1 Answers

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];
like image 198
Darin Dimitrov Avatar answered Jan 24 '26 21:01

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!