0

I am trying to pass a parameter to function to access an object's element but keep getting ReferrenceError:obj is not defined

const obj = {
  'man1': 1,
  'man2': 2,
  'man3': 3
}

function test(i) {
  console.log(obj.man `${i}`)
}
test(1)
test(2)
test(3)
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73
  • 4
    `obj[\`man${i}\`]` – luk2302 Dec 20 '22 at 12:15
  • Your code snippet doesn't produce the error "ReferrenceError:obj is not defined", it produces something along the lines of "TypeError: obj.man is not a function". The error you're describing would occur if you don't have a variable called `obj` defined anywhere, but you're trying to use it – Nick Parsons Dec 20 '22 at 12:19

0 Answers0