Curious about this object

Hi I’m doing some JS learning from a book and have come across this object example. I’d like to know why the variable is declared as two words separated by a comma. What is the purpose of this? It looks like the word ‘property’ relates to the keys but why is it needed? Thanks

  (function () {
            let property, car = {
                make: 'Jeep',
                model: 'Wrangler',
                accelerate: function () {
                    return this.model + ' drives away'
                },
                brake: function () {
                    return this.make + ' pulls up'
                }, 
}) ()

Hi @nicolahearn
That’s the declaration of two variables. Using a comma rather than a semicolon saves you writing the keyword ‘let’ a second time. What that does is the same as this in long form:

let property;
let car = {
                make: 'Jeep',
                model: 'Wrangler',
                accelerate: function () {
                    return this.model + ' drives away'
                },
                brake: function () {
                    return this.make + ' pulls up'
                }, 
}

Hmm, ok, but when I do

console.log(property) it returns ‘brake’

and when I do

console.log(car) it returns everything within the curly braces after ‘car’ whic is what I’d expect. Do you know why this is?

The code you posted has a syntax error, it has one opening brace more than closing ones. When you log it with this error in the function, it throws a syntax error. So I would have to see the correct code to tell for sure…

Ok, thanks for looking, this is the full code:

 (function () {
            let property, car = {
                make: 'Jeep',
                model: 'Wrangler',
                accelerate: function () {
                    return this.model + ' drives away'
                },
                brake: function () {
                    return this.make + ' pulls up'
                },
                
                
            }
            for (property in car) {
                console.log(property + ': ' + car[property])
            } 
            car.make = 'Ford',
                car.model = 'Bronco',
                car.engine = 'V6',
                car.start = function () {
                    return this.make + ' motor is running'
                }
            console.log('\nCar is a ' + car.make + ' ' + car['model'])
            console.log('Engine Type: ' + car.engine)
            console.log(car.start());
            console.log(car.accelerate());
            console.log(car.brake());
            console.log(property);
            console.log(car);
           
            }) ()

Ah, ok. The information above regarding comma separated variable declarations applies here. With the for loop, you assign the keys of car to the variable ‘property’. But each loop overwrites the variable, so it has the last key as a value.
If you comment out the for loop, property is ‘undefined’.

Ok, I see how the loop is overwriting but I still don’t get why we need the property word in the object in the first place. Is it just so that we can use it in the loop?

Also, if the loop wasn’t there am I still right in thinking that the word 'property in the variable declaration relates to the keys within the object whereas the word ‘car’ relates to the object as a whole (everything within the curly braces)?

Why does it return undefined? If the purpose of the commas is just to give one variable two different names, shouldn’t both of those words return the entire object?

This

let property; // <-- undefined
let car = { // <-- object
...
}

is the same as this:

let property, // <-- undefined
car = { // <-- object
...
}

The variable property has just been declared but not assigned a value and therefore isn’t related to anything.

In this case: yes. You could also write:
for (let property in car)
and leave the variable declaration at the top. But then, if you’d log property below the loop it would still log ‘undefined’ instead of ‘brake’.