0

I have two examples . In the first example :

a. 
    1. var object = {1 : "value"};

       alert(object[1]);

    2. var object = {1 : "value"};

       alert(object["1"]);

In both of the examples , the output is "value". I read in the books that object[1] will find a variable 1 and substitute the value with that. Since 1 cannot be declared as variable name in javascript (var 1="some var" //not allowed) , is it just alert(object[1]) tries to find the string declared in var object = {1 : "value"}; and alerts "value".

Because , there is no difference between 1. and 2. example alerts yield the same result.

b.
   1. 
    var object = {a : "value"};

    alert(object["a"]);

        The above example is pretty much clear that it is finding out string "a".

    2. 
    var object = {a : "value"};

    alert(object[a]);

The above example is an error , since we havent declared

var a = "some";

I am just curious to know the difference between a. 1 and a.2 and also if my understanding is correct wrt these examples?

1
  • In second example, interpreter is looking for variable a Commented Sep 6, 2016 at 10:14

2 Answers 2

3

In both of the examples , the output is "value". I read in the books that object[1] will find a variable 1 and substitute the value with that.

No. It takes a string.

If you pass it a number literal, it converts the number to a string.

If you pass it a string literal, it uses the string literal as a string.

If you pass it an variable then it gets the value of that variable and converts it to a string if it isn't already one.

1 is not a variable name. The grammar of JavaScript requires that it be treated as a number literal.

I am just curious to know the difference between a. 1 and 2

In case 1, you are passing a number literal. In case 2, you are passing an undeclared variable. You get a ReferenceError when you try to get a value from an undeclared variable.

You'd get the same effect in any other context where you were doing something with a value.

var foo = 1; // Assigns 1
var foo = bar; // Throws a reference error because bar is undeclared
Sign up to request clarification or add additional context in comments.

Comments

1

Two different things going on.

First, object keys are always strings, but you are allowed to write them without quotes in cases where that doesn't lead to syntax problems.

{1: "value"}
{a: "value"}

These are other ways of writing

{"1": "value"}
{"a": "value"}

Even if a happens to be a variable, when used like that in an object literal it justs means the string "a" and has nothing to do with the variable.

var key = "hmm";
var object = {key: "value"}; // object is now {"key": "value"}

If you want to have the value of the variable as property, you can use the following ES6 syntax in modern browsers:

object = {[key]: "value"}; // object is now {"hmm": "value"}

The other thing is what Quentin said; when you lookup a property's value with object[x], whatever x is will be converted to a string.

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.