Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  3] [ 5]  / answers: 1 / hits: 45355  / 13 Years ago, thu, august 18, 2011, 12:00:00

I have been using square bracket notation in Javascript to create and call associative arrays.



In this example, I understand that square bracket notation allows you to use a variable to call a certain object in the array.



How would you do something like this in dot notation?



var item = {};
item['1'] = 'pen';

var x = 1;

console.log(item[x]); // console will show 'pen'

More From » javascript

 Answers
22

You can't use variables in dot notation (short of using eval, which you don't want to do). With dot notation the property name is essentially a constant.



myObj.propName
// is equivalent to
myObj[propName]

[#90555] Tuesday, August 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isaac

Total Points: 645
Total Questions: 109
Total Answers: 96

Location: Cayman Islands
Member since Fri, Mar 4, 2022
2 Years ago
;