javascript - understanding Complex inArray ternary operator -


i going through inarray method code , came across following ::

inarray: function (elem, arr, i) {     var len;      if (arr) {         if (indexof) {             return indexof.call(arr, elem, i);         }          len = arr.length;         = ? < 0 ? math.max(0, len + i) : : 0;          (; < len; i++) {             // skip accessing in sparse arrays             if (i in arr && arr[i] === elem) {                 return i;             }         }     }      return -1; }, 

now understand how tenary operators work , can tell me , how below line of code works ? ternary operator ?

i = ? < 0 ? math.max( 0, len + ) : : 0; 

or kind of new construct in js ?

thank you.

alex-z.

original statement:

i = ? < 0 ? math.max(0, len + i) : : 0; 

to understand better,

i = ? (i < 0 ? math.max(0, len + i) : i) : 0; //      ^                                ^ 

yes, nested ternary operator ? :.

following if else representation of above statement, represented in if..else step step.

if (i) {     = < 0 ? math.max(0, len + i) : i; } else {     = 0; } 

it works follow:

if (i) {     if (i < 0) {         = math.max(0, len + i);     } else {         = i;     } } else {     = 0; } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -