0

when I write

for (var x in object) { 
  console.log(object[x]) 
} 

it works fine!
But if I replace object[x] with object.x it doesn't work, it returns undefined.


console-logging both object[propriety1] and object.propriety1 outside of for in loop works fine for both of them.

Any explainations?!

var object = {
         propriety1 : "value1",
         propriety2 : "value2",
         propriety3 : "value3"
}
     
for (var x in object) {
  console.log(object.x);
}
     
console.log(object.propriety1);

0

4 Answers 4

1

When you have a for loop it gives you all the keys of an object.

object [key] is the syntax to get respective values

var object = {
         propriety1 : "value1",
         propriety2 : "value2",
         propriety3 : "value3"
     }
     
     for (var x in object){
         
         console.log(x);// gives you keys of object
         console.log(object[x]);//syntax to get value corresponding to a key
     }
     
     console.log(object.propriety1);

Sign up to request clarification or add additional context in comments.

Comments

1
  1. x is just the name of the variable in the for loop

The value of x will be "property1", then "property2" and then "property3" as the for loop advances

  1. object[x] means "get a property of object whose name is contained in the variable x". It is equivalent to object["property1"], etc

  2. object.x is meaningless, unless your object has a property with name "x"

Comments

0

The Reason for this quite simple Actually.

When we use for-in Loop to iterate over an object, the variable gets assigned the string values(i.e variable x in the above example gets string values), and when we try to access object.x inside loop what actually happens is object."property1" and there is no property named "property1" (notice the quotes) in the object.

2 Comments

how about when we try to access object[x] ?? doesn't it inject the quotes inside the brackets too ?
When we use the [key] syntax, The JS Engine always convert the key passed into string. So it doesn't matter.
0

Explanation

When you write object.x the compiler is looking for a property x but it doesn't find it and so it returns undefined, because there is no x property.

With object[x] the compiler is looking for a property in object with the value that x stores.

Example

var object = {
  propriety1 : "value1",
  propriety2 : "value2",
  propriety3 : "value3"
}

// object doesn't have the property x --> returns undefined
console.log('first', object.x)

// object has propriety1 so it returns it value
console.log('second', object.propriety1)
     
for (var x in object) {
  // looks for the value of x in object
  console.log(object[x]);
  
  // looks for the property x in object --> undefined
  console.log(object.x)  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.