Solved

Accessing property value in an object.

Posted on 2016-11-12
1
50 Views
Last Modified: 2016-11-21
I am studying 'object literals' in javascript. I am a biginner.
If object property name is using valid identifier, its value can be accessed as 1).
Otherwise, it can be accessed like 2) using "[]"
Since 2 is not valid object property name in 3), I understand foo.2 causes an error. However, it says that 'missing ) after argument list'
Can you please explain that?
Also, I don't understand why I am getting an error for 4)
And 5) and 6) are valid statements.
What's the difference between foo[a] and foo["a"]?
what's the difference between foo[2] and foo["2"]?

var foo = {a: "alpha", 2: "two"};
console.log(foo.a);    // alpha ---------1)
console.log(foo[2]);   // two ---------2)
//console.log(foo.2);  // Error: missing ) after argument list -----------3)
//console.log(foo[a]); // Error: a is not defined-----------------4)
console.log(foo["a"]); // alpha --------------5)
console.log(foo["2"]); // two -------------6)

Open in new window

0
Comment
Question by:Isabell
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41885186
console.log(foo.2);  // Error: missing ) after argument list -----------3)
the interpreter think you miss something to have a valid statement, this is for it an right parenthesis

//console.log(foo[a]); // Error: a is not defined-----------------4)
the right syntax is to put simple or double quotes around "a":
console.log(foo["a"]); // or console.log(foo["a"]);
or you must define << a >> as any other variable, for example :
var a = 2;
console.log(foo[a]);   // two ---------2)
var a = "a";
console.log(foo[a]);    // alpha ---------1)


sometime you want to use a key as variable
in foo[a] , a is a variable
in foo["a"], "a" is a key

it's not comparable to {a:1234}
we usualy don't add the quotes, as the interepreter know << a >>, the member on the left side of << : >> gonna be always a key
you can't use a variable to define a key using {}, you gonna use :
var foo = {};
var var1 = "a";
var var2 = "2"; // or 2
foo[var1] = "alpha";
foo[var2] = 'two';
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ModalPopup  question 22 39
links not working 2 27
Can't get js code to execute 8 32
converting document.write() to innerHTML 7 51
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question