0

Normally we can declare variable via:

var myVar1 = "Hello";
var myVar2 = true;

When put it into object way, it will look like this:

var globalVar = {
   myVar1: "Hello",
   myVar2: true
}

How can I declare a variable without assigning anything, e.g. var myVar3; in the object way?

Thank you.

1
  • Do what now? Do you mean globalVar.myVar3 = null; Commented Nov 16, 2013 at 13:47

2 Answers 2

5

How can I declare a variable without assigning anything, e.g. var myVar3; in the object way?

You can't. But you can get the same result:

var globalVar = {
   myVar1: undefined,
   myVar2: undefined
};

When you declare a variable with var, the variable's initial value is undefined. So the above does the same for object properties.

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

Comments

0

Using undefined is technically correct, but has little to no point, you'd be better off declaring the variable with a NULL consider this code:

            var obj = {
                oUn : undefined,
                oNull : null
            };

        document.body.appendChild(document.createTextNode(
                typeof obj.oUn 
                + '  ' + typeof obj.oNull 
                + ' ' + typeof obj.doesNotExist
        ));

Although yes, in FireBug, and maybe other debuggers (did not check) obj.oUn does come up, but if you try to check it within JS you get the same result as if its not even defined. (Note that the "typeof obj.doesNotExist" returned the same value as "typeof obj.oUn" although, it was not defined). Though that said if you check it within a for loop you do get it i.e. this will display the obj.Un

            var obj = {
                oUn : undefined,
                oNull : null
            };

        document.body.appendChild(document.createTextNode(
                typeof obj.oUn 
                + '  ' + typeof obj.oNull 
                + ' ' + typeof obj.doesNotExist
        ));

            document.body.appendChild(document.createElement('br'));

            var name;
            for(name in obj) {
                document.body.appendChild(document.createTextNode(name));
                document.body.appendChild(document.createElement('br'));
            }

So I guess it depends on why you need it. By in large I would say if you do not need the name per say (i.e. your code already knows the name) then do not even declare it and simply use

var globalVar = {};

You can always extend on it latter once you need it or if you need it, then use NULL, because you can actually check in JS if its in use by checking if the value is NULL or not. With undefined you'd have no way of knowing, because it does not differ at all from a value that is not in use and you'd need to make a cumbersome helper function in order to find out.

5 Comments

You seem to be confusing the concepts of something that is undefined, with something that is defined but has the value undefined. typeof makes no distinction between them, but nearly everything else does. (There are lots of things typeof doesn't distinguish, like Date vs RegExp.) But there is a fundamental difference between an object not having a problem and having the property with the value undefined, and that difference shows up nearly everywhere (for-in loops, the in operator, Object.keys, hasOwnProperty, ...).
True enough, but that why I said it has little to no point. I am not saying its completely useless, but more in the lines that most of the time at least I would go with NULL because I could easily check for it with a single statement. I used typeof just as an example to illustrate my point but even obj.doesNotExist === undefined and obj.oUn === undefined are identical, with is my original point, and if I would try something like obj.oUn instanceof undefined I would only get an error. I do not know, maybe I am missing something and there is a simple check for it. But at least I do not know it
@T.J.Crowder Sorry for the second comment. I feel like I need to explain a bit more, because I have a rep of 1 vs 225k. It's just in my case I've always used undefined to check for existence of a variable in an object, for me giving the value of undefined to an object member would break some of my code's logic. It not unusual for me to check for something like obj['dynamicVariavbleName'] == undefined. It depends on the situation, but using undefined as the value would rob me of the check.
@ Illimar: "obj['dynamicVariavbleName'] == undefined" Just to give you another tool for your belt, you can use dynamicVariavbleName in obj to find out if obj has a property called dynamicVariavbleName. That will be true if the property exists (even if it has the value undefined), false if it doesn't. in will also check the object's prototype (like your code does). If you don't want to check the object's prototype, obj.hasOwnProperty("dynamicVariavbleName"). But in is closer to what you're doing now. Again, just to add tools! :-)
@ Illimar: Sorry, my statement above "you can use dynamicVariavbleName in obj to find out if obj has a property called dynamicVariavbleName" is slightly misleading. What I meant was "...to find out if obj has a property with the name in the dynamicVariavbleName variable." So 'foo' in obj tells you whether obj has a property called foo. If you have a variable, f, with the contents "bar", then f in obj will check if obj has a property called bar. Best,

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.