JavaScript > dot vs bracket notation

the case

the question is the difference between dot and bracket object notation in JS

Compare arrays and objects as data structures in JS?

  • both store multiple values
  • arrays: you use the index number to access the array item — their organizational principle is that they store values in an ordered list
  • objects: you use keys to access the object’s properties — their organizational principle is that they store values in key-value pairs

Properties

  • there are 2 ways of accessing properties
  • dot notation . - takes the input as a literal name of the property
  • square bracket notation [] - evaluates the input first and that evaluation is considered to be the name of the property
  • you can’t access array’s items (= properties!) with dot notation only with [] notation
  • you can access object’s items with . dot notation or with bracket notation with quotes around them

Evaluation of the bracket notation

  • property in the [] — unless in quotes — is interpreted as an identifier first
  • you also need to use
  • evaluation means also that the property can be bound by a variable or a parameter in a for-in-loop

Written by

today I learnt… | as a support eng of the wonderful Snowplow Analytics, expect everything around modern (postmodern?) business intelligence

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store