-4

can someone explain me why this happend ?

var Danny = {
    name: 'Danny',
    lastname: 'Black',
    yearOfBirth: 1998,
    job: 'Programmer',
    isMarried: false,
};

var xyz = 'job';
console.log(Danny[xyz]);

In this case I got Prgrammer in console why?

Danny
  • 31
  • 1
  • 7
  • 2
    because `xyz` is a variable with value `job` and `Danny` is an object having a property `job` – Rishikesh Dhokare Aug 12 '18 at 15:22
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors – Matt Ball Aug 12 '18 at 15:22
  • 1
    This isn't a question you ask on Stack Overflow. Check out some basic tutorials and your question will be answered. It's like you post a question on Math SE asking why 1+1=2. Grab a book (or video), learn the basics and when you have a problem you need help with, seek the community's help. Best of luck. – Angel Politis Aug 12 '18 at 15:28

1 Answers1

1

If you use this console.log(Danny["xyz"]); then it will return undefined because there is no property with xyz in above object

If you use this console.log(Danny[xyz]); then it will replace xyz to "job" which means

console.log(Danny["job"]); that is why it is giving you "Programmer" as output.

Nishant Dixit
  • 5,388
  • 5
  • 17
  • 29