-1
function Userinfo (ad,age,city,street){
    this.ad = ad
    this.age = age
    this.city = city
    this.street = street
}

let orxan = new Userinfo ('Orxan',30,'sumqayit','sulh')

console.log(orxan[age])

why console.log(orxan[age] is not working but console.log(orxan.age) working

  • 1
    [Duplicate](//google.com/search?q=site%3Astackoverflow.com+js+bracket+notation+referenceerror) of [Why am I getting a ReferenceError: 'x' is not defined?](/q/45410307/4642212). – Sebastian Simon Jun 19 '21 at 12:55

1 Answers1

1

You must enclose age in quotes like so:

console.log(orxan['age'])

Otherwise Javascript will search for variable named age in the scope

Full snippet:

function Userinfo (ad,age,city,street){
    this.ad = ad
    this.age = age
    this.city = city
    this.street = street
}

let orxan = new Userinfo ('Orxan',30,'sumqayit','sulh')

console.log(orxan['age'])

Results in:

$node main.js
30
Deyan Georgiev
  • 343
  • 3
  • 15