Solved

Accessing property value in an object.

Posted on 2016-11-12
1
56 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

717 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