2

In javaScript Object.

var person = {
  name: "bengen",
  age: 27,
  5: true
}

How can I access the property 5, please?

-_-

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
codesve
  • 35
  • 1
  • 6
  • Also have a look at some introductory material: https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects – Felix Kling Jul 12 '12 at 15:46

5 Answers5

1

Try using []. For example, person[5] or person["5"]

Dmitry Osinovskiy
  • 9,999
  • 1
  • 47
  • 38
1

Just use the access via []:

alert( person[ 5 ] );

A JavaScript object's properties can be accessed by using . or []. The latter is especially useful for numeric keys or in cases, when you have a key identifier stored in another variable.

Sirko
  • 72,589
  • 19
  • 149
  • 183
1

Just use the square bracket accessor rather than the dot:

if(person[5]){
    // if the value is true, do something
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
1
var person = {
  name: "bengen",
  age: 27,
  5: true
}

console.log(person['5']);
Conner
  • 30,144
  • 8
  • 52
  • 73
quickshiftin
  • 66,362
  • 10
  • 68
  • 89
1

From what I remember, this should work: person["5"]

ioreskovic
  • 5,531
  • 5
  • 39
  • 70