Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Accessing property value in an object.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

598 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