0

Here is the code snippet:

var prjId = navObj.itemId || navObj

Does this mean that prjId is either equal to navObj.itemId or navObj? What does it then mean that a variable is equal to navigation object?

Thank you in advance for answers!

1

5 Answers 5

3

This is equivalent to the following:

var prjId;
if(navObj.itemId)
    prjId = navObj.itemId;
else
    prjId = navObj;
Sign up to request clarification or add additional context in comments.

Comments

3

If navObj.itemId is set to false or has not been defined at all,

prjId = navObj;

otherwise:

prjId = navObj.itemId;.

2 Comments

@p.s.w.g has the more correct answer. If navObj.itemId was set to false, it would be defined and yet would set prjId to navObj.
@Jage correct. Didn't think about that. Updated answer to cover that corner.
2

No. The || operator first tries to convert navObj.itemId to a Boolean value.

  • It will be converted to true if it is already the Boolean value, true, a number other than 0 or NaN, a non-empty string, or an object that is not null or undefined. These are known as "truthy" values.

  • It will be converted to false if it is already the Boolean value false, 0, NaN, an empty string, null or undefined. These are known as "falsey" values.

If navObj.itemId is "truthy", navObj.itemId is assigned to prjId, otherwise navObj is assigned to prjId.

Further Reading

3 Comments

I'm not js expert but that's not a ternary operator. To me it would mean that prjId is true if one of those condition is true. isn't it?
No, as I understand it, p.s.w.g's answer is not even discussing a ternary, but the /quality/ ("truthy" vs. "falsey") if you will of the items on either side of the || operator. If the /quality/ of the left side is truthy, then the variable is set to the /value/ of the left side. If not, the right side is used.
@user711061 Not exactly. The || operator in JS is a kind of simplified version of the ?: operator. x || y is equivalent to x ? x : y. The result is x or y, not the truth values associated with them.
1

It simply means that if the left operand of the logical or operator (||) is a truthy value then return it otherwise return the right operand.

The following values are always falsy:

  • false
  • 0
  • empty string
  • null
  • undefined
  • NAN (not a number)

So if navObj.itemId doesn't evaluate to anything of the above, then it will be assigned to the prjId variable.

This is widely used when we have optional parameters in a function, for example. It is a way of specifying the default value for an optional parameter. But of course that's not the only use of it.

Comments

0

It sets prjId to the property of itemId on navObj, if it exists (evaluates to 'truthy'), if it doesn't exist (or it evaluates to 'falsy'), prjId gets set to navObj.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.