Skip to main content
Make the example more exhaustive to explain the point
Source Link

There are two ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var foo = 'bar'; something[foo]; // both x = something[foo] and something[foo] = x work as expected 

var something = { bar: 'foo' }; var foo = 'bar'; // both x = something[foo] and something[foo] = x work as expected console.log(something[foo]); console.log(something.bar)

There are two ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var foo = 'bar'; something[foo]; // both x = something[foo] and something[foo] = x work as expected 

There are two ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var something = { bar: 'foo' }; var foo = 'bar'; // both x = something[foo] and something[foo] = x work as expected console.log(something[foo]); console.log(something.bar)

added 74 characters in body
Source Link
Salman Arshad
  • 273.5k
  • 85
  • 450
  • 540

There are two ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var foo = 'bar''bar'; something[foo]; // both x = something[foo] and something[foo] = x work as expected 

There are two ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var foo = 'bar' something[foo] 

There are two ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var foo = 'bar'; something[foo]; // both x = something[foo] and something[foo] = x work as expected 
Update documentation link
Source Link
JLRishe
  • 102.1k
  • 19
  • 138
  • 171

There are two ways to access propertiestwo ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var foo = 'bar' something[foo] 

There are two ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var foo = 'bar' something[foo] 

There are two ways to access properties of an object:

  • Dot notation: something.bar
  • Bracket notation: something['bar']

The value between the brackets can be any expression. Therefore, if the property name is stored in a variable, you have to use bracket notation:

var foo = 'bar' something[foo] 
Rollback to Revision 4
Source Link
JLRishe
  • 102.1k
  • 19
  • 138
  • 171
Loading
added 8 characters in body
Source Link
Michał Perłakowski
  • 93.4k
  • 30
  • 165
  • 189
Loading
use the original object's variable name
Source Link
Bergi
  • 670.6k
  • 162
  • 1k
  • 1.5k
Loading
simplify based on question simplification
Source Link
Andrew Marshall
  • 97.3k
  • 20
  • 228
  • 217
Loading
added more details
Source Link
Felix Kling
  • 820.1k
  • 181
  • 1.1k
  • 1.2k
Loading
Source Link
Jan Hančič
  • 54k
  • 17
  • 99
  • 101
Loading