0

I have one object as below formate.

var pqr = {'1' : 'a1','b': 'b1'}
eval('pqr.1') //throw exception
eval('pqr.b') //This is working fine.

Please let me know why eval function throw an exception?

chirag
  • 1,818
  • 1
  • 15
  • 36

1 Answers1

1

You dont need eval here , if you still want to use it , use square braces , and to access b put it in a quotes otherwise b will be considered as variable

var pqr = {
  '1': 'a1',
  'b': 'b1'
}
console.log(eval('pqr[1]')) //throw exception
console.log(eval('pqr["b"]'))
brk
  • 48,835
  • 10
  • 56
  • 78