7.Search for a friend


#1



https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#


1)
var search = function(name){
    for(var key in friends){
        if(name===friends.key.firstName){
            console.log(friends.key);
            return friends.key;
        }
    }
}


2)
var search = function(name){
    for(var key in friends){
        if(name===friends[key].firstName){
            console.log(friends[key]);
            return friends[key];
        }
    }
}

What's the difference between first and second?


#2

The above with throw a reference error. key is a dynamic variable, not very likely to be "key", but any of the defined property names.

This treats key as a dynamic variable, representing a subscript of the friends object. This is also referred to as associative array syntax.


#3

@alibek.sharapov I was wondering this too... I did find this result through google from the old forum.

Really, there is no difference. It's just a matter of preference. Using bracket notation can be very flexible though. If you're doing a for/in loop, you can use bracket notation to loop through every single property in an object without knowing what properties are there beforehand (see example below).


Try to read Working with objects at JavaScript Guide.
It is indirectly said that square bracket notation should be used when property names are to be dynamically determined (when the property name is not determined until runtime).

The section in question:

An object property name can be any valid JavaScript string, or anything that can be converted to a string, including the empty string. However, any property name that is not a valid JavaScript identifier (for example, a property name that has a space or a hyphen, or that starts with a number) can only be accessed using the square bracket notation. This notation is also very useful when property names are to be dynamically determined (when the property name is not determined until runtime).


Update

@mtf: The above with throw a reference error. key is a dynamic variable, not very likely to be "key", but any of the defined property names.

Am I understanding correctly that that means dot notation can only be used when the property name is known? e.g. for property propertyKey: propertyValue

var x = propertyKey
console.log(obj.propertyKey)

is okay, but where var x is a place holder for all potential property keys within an object (and not a variable assigned to a specific key, nor a specific key's name) console.log(obj.propertyKey) is not okay?


Why does bracket notation for accessing object properties work where dot notation doesn't?
Bleep Bleep: Variable is to Global Variable as This.Key is to...?
#4

as in, 'defined', yes. We may define a property on the fly with dot notation, but we cannot access a variable using 'property specific notation'. It is undefined in the normal sense.

var object = {};
var object.property = "some value";

In the above, we define a property on the fly. It didn't exist before, but now it does.

console.log(object.hasOwnProperty('property'));    // true

In a dynamic scenario, we are 'winging it' by utilizing a variable representation of each key that is 'found in' the object with `for..in'.

for (var key in object) {
    console.log(key);                           // property
    console.log(object[key]);                   // some value
    console.log(object.hasOwnProperty('key'));  // false
}

#5

@mtf I see, so in terms of accessing a property, dot notation can be thought of as "property-specific notation", is that right?

And so then the way "property-specific notation" works, my first example actually would return a Reference Error?

var x = propertyKey
console.log(obj.propertyKey)

--> obj.x =/= obj.propertyKey?

Thank you for your help!


#6

Yes.

And yes, again.

The distinction is fairly easy to make once we picture things in our mind, but then I am getting poetic, and digress...

If x is propertyKey, as in,

var james = {
    job: 'programmer',
    married: false
}
var x = "job";

console.log(james.x);    // referenceError

console.log(james[x]);   // programmer

#7

The distinction is fairly easy to make once we picture things in our mind, but then I am getting poetic, and digress...

Would that I were telepathic... Thank you so much for your help and patience, @mtf. I understand the difference between dot notation and bracket notation much, much better now. If only I could go back in time and tell myself at the beginning of the object chapter...! :slight_smile:


#8

Then we wouldn't need 'learning'.