Solved

Accessing property value in an object.

Posted on 2016-11-12
1
17 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
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now