Skip to content

Latest commit

 

History

History

dots-vs-brackets

Dots vs Brackets

There are two primary ways to access properties in JavaScript objects:

  • Dot notation: direct access by key
  • Bracket notation: indirect access by value

These two types of access can be quite confusing at the beginning, but with a solid understanding of how variables & values work the mystery fades away. Knowing when to use one or the other can take some time and experience, but learning how they work is not so tricky with the right visualizations.


The Code

var og = {first: 'fifty', last: 'cent'};

var curtis = 'first';
var jackson = 'last';

console.log(og.first); 		// log: fifty
console.log(og.last); 		// log: cent

console.log(og[curtis]);	// log: fifty
console.log(og[jackson]);	// log: cent

Live PyTut link

Gist for further study

Direct vs Indirect access:


The Sketches