0

I've seen some code in the form of:

var vendorcode = vendorcode || {};

I've always thought that || was a logical operator. But this one is a real doozy.

1

1 Answer 1

9

This code assigns {} to vendorcode if vendorcode is false-y. Meaning it's undefined, false, 0, null, etc.

If vendorcode is not false-y it'll keep its value.

You can read it out loud as: "vendorcode equals vendorcode OR {}"

Sign up to request clarification or add additional context in comments.

1 Comment

I figure it had to do something with an assignment. I didn't realize || could be used with an assignment of a function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.