Skip to content Skip to sidebar Skip to footer

What Is The Issue In This Javascript Short Circuit Assignment

the following javascript code is getting giving undefined as the final output. But as far as I know the OR ' || ' operator will stop the evaluation as soon as it gets 'true'. But i

Solution 1:

In one word: operator precedence.

Yes, || short-circuits and doesn't evaluate the second half of the expression used as condition in the ternary operator.

field.ipaddr || (field.ip6addr && field.ip6addr != '::') ? .. : ..

Evaluates to:

'0.0.0.0' ? .. : ..

Which evaluates to true and then evaluates the true branch of the ternary operator:

field.ip6addr

If you want a different logical grouping, use parentheses:

field.ipaddr || (.. ? .. : ..);

Solution 2:

I'm assuming you know that you've written field.ipaddr instead of fields.ipaddr. Probably for testing purposes. If the last part of the expression is your problem, write it like so:

(undefined || fields.sysid);


Solution 3:

I don't think so is there any property with name ip6addr in current object?

var bestName = (field.ipaddr || (field.ip6addr && field.ip6addr != '::') ? field.ipaddr : undefined || field.sysid);
undefined

Either you should add ip6addr in current object or replace with appropriate property


Post a Comment for "What Is The Issue In This Javascript Short Circuit Assignment"