[] Notation in JavaScript

let details = new Map([
  [new Date(),"today"],
  [2, {javascript: ["js","node","react"]}]
])

Above is one example where both notations are used.

Object literals {} and [] holds a very important position in Javascript. I am confused with the application of square notations. They are also used in JSON and also object literals are used in JSON.

Those who are experienced today would have gone through the same confusion during their evolution. How to understand everything about these two brackets in JS to apply them with clarity every time.

As far as I understand square bracket notation represents a string.

What do you mean with “square notation”? Never heard of that and also the internet does not know this …

Btw…

let details = new Map([
[new Date(),“today”],
[2, {javascript: [“js”,“node”,“react”]}]
])

Whoever will create such a map should be tarred and feathered…

Only {} is an object literal, [] is an array. So just a list of things.

Like ['apple', 'orange', 'banana'].

An object is used to describe properties of something, like

{name: 'apple', color: 'green'}

And if you want you can combine the two

[{name: 'apple', color: 'green'}, {name: 'banana', color: 'yellow'}]

That’s an array with a green apple and a yellow banana.

It does not. Single or double quotes represent strings, i.e., 'apple' or "apple".

It’s just an example, no need to get mad.

2 Likes

As far as I understand square bracket notation represents a string.

Maybe I have got the wrong end of the stick here but are you thinking along the lines of

obj.prop
// vs
obj['prop']

By using bracket notation the property can be a dynamic value

const obj = {
  firstName: 'Cholmondley',
  lastName: 'Warner'  
}

const propName = 'firstName'

obj[propName] // Cholmondley
obj.propName // undefined

also things that would otherwise be invalid in dot notation but are valid property name identifiers - spaces, symbols,leading numbers, expressions, etc.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.