0

What does window['someKeyword'] mean in JavaScript?

I guess the window is the DOM window object, but the syntax to retrieve a property is window.propertyName in the reference I found, not window['propertyName']. Are these two ways to retrieve the property?

George
  • 3,384
  • 5
  • 40
  • 64

1 Answers1

5

To access a field of an object in Javascript there are two ways.

let's this is an object

obj = {name:'Object1'};

You can access name field by two ways

obj.name // returns 'Object1'
obj['name'] //returns 'Object1'

besically [] this method is useful when you have to access a field using a variable like bellow

var obj = {name:'Object1'}
var field = 'name'

obj[field] //returns 'Object1' because it will put value of field in `[]`
Mritunjay
  • 25,338
  • 7
  • 55
  • 68