Need Explanation.. For In loop


#1

Here is code...

var nyc = {
fullName: "New York City",
mayor: "Bill de Blasio",
population: 8000000,
boroughs: 5
};

// write a for-in loop to print the value of nyc's properties
var fn="fullName"
var my="mayor"
var pp="population"
var brg="boroughs"

for (var fn in nyc) {
console.log(nyc[fn]);
}

The Result is it prints out all values of properties of NYC object.. For here it's okay..
But I assign a variable for each property, then write a loop for NYC object.
When I delete the my, pp and brg variable the code isnt work. I need to assign all why? and why I need to console.log only one variable which I assign later with the property of NYC object. not others?


#2

May I ask what the "fn, my, pp, and brg" are for?


#3

I want to print out only the values of an Object Properties. Therefore I assigned an Variables and assigned them a property name of an Object. after I call the object with variable..


#4

for (var fn in nyc) {

let me explain what's happening here. I'll translate this line into English.

for every new variable named fn, inside of the object nyc (fullName, mayor, population, boroughs). I want to do this.

meaning you're not using the variable fn that you made in this line var fn="fullName" you're actually making a new variable under the same characters that will only be used in the for loop.


#5

so As I understand I don't need to assign any variable to the values of object properties? yes?

Really, You are right I tried. and it works.. thanks. I thought that I need variables.