?
Solved

Accessing property value in an object.

Posted on 2016-11-12
1
Medium Priority
?
65 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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
Suggested Courses

771 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