2

I have this code in JavaScript:

var JSON;
JSON||(JSON={});

Can you tell what this code is doing?

2
  • If that's the snippet exactly, then it's a very roundabout way of doing var JSON = {};, which creates a new object and assigns it to the variable JSON. Commented Nov 30, 2011 at 18:53
  • This question is similar to: What does the || (logical OR, double vertical line) operator do with non-boolean operands?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Nov 12, 2024 at 13:01

6 Answers 6

3

var JSON is declaring a global (or other scope) variable.

JSON||(JSON={}); is first checking to see if JSON evaluates to true or false, and if false, then JSON is set to an empty object;

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

2 Comments

It doesn't check to see if it has a value. It could have false, 0, "" or a number of other things and still get an empty object assigned.
@Quentin - oops "has a value, if not" should have been "evaluates to true or false, and if false"
2

It is pretty pointless in that context,

var JSON;

creates a variable named JSON

The second part,

JSON||(JSON={});

is equivalent to

if(!JSON){
    JSON = {};
}

This can all be skipped and written as

var JSON = {};

3 Comments

If you skip the test, then you will always overwrite whatever it contains. That's usually Not A Good Thing in any case where someone has bothered to check if the variable has a truthy value.
@Quentin that only applies if there is more code before the set, which there is not.
— I think that assuming the code in the question represents the complete program is probably unsafe.
1

It scopes a variable called JSON, then uses the shortcircuit nature of the || operator to assign an empty object to said variable unless that variable has a truthy value.

4 Comments

@Robotsushi - thats what its called. theres also falsey.
@Robotsushi: Isn't that the official term?
I just don't hear those expression used often enough =D
@DanielA.White: I thought it was "falsy." :)
0

Its just making JSON an empty object.

Comments

0

This code is doing the following:

  1. Declare variable JSON (JSON === undefined)
  2. Assign an empty object ({}) to JSON if JSON is a falsey value.

Falsey values include: null, undefined, "" (empty string), 0, NaN, false.

JSON has been declared via var JSON, so is set to undefined, meaning that the right hand-side of the operation JSON||... will be executed. In other words, all the code is achieving is:

var JSON = {};

4 Comments

It scopes the variable. It might have a value from somewhere else.
@Quentin, Exactly -- the value must be undefined because the variable is declared via var JSON. If it had another value prior to that declaration it won't matter since JSON||.. runs after that.
— No, var gets hosted and doesn't reset a variable with a value defined earlier in the scope. I've seen: function (foo) { var foo … } a number of times. The var is redundant because arguments are locally scoped automatically.
Ah I see. I didn't think of the case where the var would have already been defined in the same scope.
0

I think this says : if the var 'JSON' is null create a blank javascript obect.

1 Comment

No. It tests if it is null or undefined or any other falsy value. and if it is it assigns a JavaScript object. There is no JSON there (except as the name of a poorly named variable).

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.