0

as u can see below , obj.key outside the for in loop is working properly but why not in the for in loop ?

and obj[key] inside the for in loop is working properly , what`s the reason behind one is working correctly and other one is not ?

code

let obj = {
  name: "lucky",
  hobbies: "coding"
}
console.log(obj.name)
for (let key in obj) {
  console.log(obj.key)
}

output

lucky
undefined
undefined
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
lucky
  • 31
  • 8
  • 1
    Does this answer your question? [Add a property to a JavaScript object using a variable as the name?](https://stackoverflow.com/questions/695050/add-a-property-to-a-javascript-object-using-a-variable-as-the-name) – Ivar Jul 06 '22 at 07:45
  • 3
    you should call same as : `obj[key]` – Xupitan Jul 06 '22 at 07:46
  • @xupitan yeah thats y i mentioned that too , but i am asking why object.key is not working – lucky Jul 06 '22 at 07:47
  • Because `key` is not the _name_ of a key and can't be accessed like a named key. – Andy Jul 06 '22 at 07:48
  • because you are fetching the property name called 'key' from the object inside the obj variable. While if you want to fetch a property using a dynamic name given from a string you need to use the accessor `[]` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors – Diego D Jul 06 '22 at 07:48
  • `key` of `obj.key` !== `key` of `let key in obj` – Xupitan Jul 06 '22 at 07:50
  • @andy it is through let key in obj , each time it iterates it assigns keys from the object right – lucky Jul 06 '22 at 07:53
  • @ivar that is adding a property right and this is retrieving a property – lucky Jul 06 '22 at 07:55
  • @lucky The dot is a property accessor that looks for a literal `key` in your object. Your object contains a `name` and `hobbies` key, not a `key` key. The bracket notation allows you to access a property using a JavaScript value such as the string the `key` variable evaluates to. – Ivar Jul 06 '22 at 07:57
  • 1
    Alternative duplicate: [JavaScript property access: dot notation vs. brackets?](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – Ivar Jul 06 '22 at 07:58
  • @xupitan why both are not equal xuptian – lucky Jul 06 '22 at 07:59
  • 1
    Because that's what the JavaScript Language architects decided @lucky. There really isn't much more to it. – Ivar Jul 06 '22 at 08:00
  • @lucky it's a _variable_ that's been assigned the value of the current key in the iteration. "key" is not a named key in the object and, therefore, you need to use `obj[key]` to access the value. Read that second dupe that @Ivar added. – Andy Jul 06 '22 at 08:02
  • you can also call it like this: eval(`obj.${key}`) :)))) – Xupitan Jul 06 '22 at 08:06
  • 1
    But you shouldn't when there's a far simpler method that doesn't involve `eval`. – Andy Jul 06 '22 at 08:07

1 Answers1

1

For accessing object if you using dot operator then you have to give the key directly and you can not use variable name for that. eg => obj.name but you can not use obj.key as it conside key as a key name. You can user [] operator to access the key in the for loop. Thus your code will be =>

let obj = {
  name: "lucky",
  hobbies: "coding"
}
for (let key in obj) {
  console.log(obj[key])
}