JavaScript Bracket Notation | Property accessors

  • by

JavaScript Bracket Notation is a Property accessor and it provides access to an object’s properties. Property identifiers have to be a String or a variable that references a String.

object['property']

JavaScript Bracket Notation examples

Simple example code access properties on an object by specifying the name of the object followed by the property name in brackets.

<!DOCTYPE html>
<html>
<body>

  <script>
   let obj = {
    cat: 'meow',
    dog: 'woof'
  };

  let sound = obj['cat'];
  console.log(sound);
</script>

</body>
</html>

Output:

JavaScript Bracket Notation

Bracket notation when working with Arrays

syntax: arrayName[element]

let arr = ['a','b','c'];

let letter = arr[1];

console.log(letter); // b

Do comment if you have any doubts or suggestions on this JS Notation topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.