What is the difference between giving character with quotes as key and just character as key of property of an object in javascript

  1. const first= {a: 2, b: 3}

  2. const second= {"a": 2, "b": 3}

Even though both examples given above print the same in console, I can’t access the values of those objects the same way. To access the key of first object, I have to write console.log( first.a ) and for second object console.log( first[a] ).

So if they are printing the same, why should I have to access them in different way. If the keys in first object aren’t characters then what are they…

Below code is the reason why I got this question

const maxchar = (str)=>{
  let charCount = {}
  for (let index = 0; index < str.length; index++) {
    const char = str[index]
    charCount[char] = charCount[char]+ 1|| 1 //can't access char as charCount.char
  }
  return charCount
}

79 thoughts on “What is the difference between giving character with quotes as key and just character as key of property of an object in javascript”

Leave a Comment