I was just going through the inArray method code and came across the following ::
inArray: function (elem, arr, i) {
var len;
if (arr) {
if (indexOf) {
return indexOf.call(arr, elem, i);
}
len = arr.length;
i = i ? i < 0 ? Math.max(0, len + i) : i : 0;
for (; i < len; i++) {
// Skip accessing in sparse arrays
if (i in arr && arr[i] === elem) {
return i;
}
}
}
return -1;
},
now i understand how tenary operators work , but can somebody tell me , how the below line of code really works ? is it even a ternary operator ?
i = i ? i < 0 ? Math.max( 0, len + i ) : i : 0;
or is it some kind of a new construct in JS ?
Thank you.
Alex-z.
Original Statement:
To understand it better,
Yes, this is nested
ternary operator
? :
.Following is the
if else
representation of the above statement, represented inif..else
step by step.It works as follow: