I am trying to understand the difference between .Notation
and []
notation. In my problem below when I use if (object[key] === true)
I get the correct answer. When I use if (object.key === true)
it does not work. Could someone explain why it's different.
var myObj = {
one: false,
two: false,
three: false,
four: true,
five: false,
six: false
};
var myFunc = function (object) {
for (var key in object) {
if (object[key] === true) {
return "There is a true value in this object";
} else {
}
}
return "Sorry, there are no true values in this object";
};
Dot notation is faster to write and easier to read than bracket notation. However, you can use variables with bracket notation, but not with dot notation. This is especially useful for situations when you want to access a property but don't know the name of the property ahead of time.
Dot notation is one way to access a property of an object. To use dot notation, write the name of the object, followed by a dot (.), followed by the name of the property. Example: var cat = { name: 'Moo', age: 5, }; console.
[] is an empty array (i.e. with zero items). {} is an empty object (i.e. with zero key-value pairs).
JavaScript objects have internal slots or properties that can't be accessed in the usual way. They have property names that are surrounded by double square brackets [[]] and create available when the object is created.
When you use dot notation, key
means the actual property in the object, which will not be there. So, undefined
is returned which is not equal to true
.
When you use []
notation you are accessing the property in the object with the name in the variable key
. So, that will work.
For example,
var myObj = {
myVar : 1
};
for (var key in myObj) {
console.log(key);
console.log(myObj.key);
console.log(myObj[key]);
}
This will print,
myVar
undefined
1
Because, myObj
has no member named key
(myObj.key
tries to get the member with the name key
) and in the next case, myObj
has a member named myVar
(myObj[key]
tries to get the member with the value in key
).
Dot Notation
jslint prefers dot notation.
[] Notation
This offers flexibility. You can dynamically access the members with a variable.
Dot notation is faster to write and clearer to read.
Square bracket notation allows access to properties containing special characters and selection of properties using variables.
<form id="myForm">
<div><label>
<input type="checkbox" name="foo[]" value="1"> 1
</label></div>
<div><label>
<input type="checkbox" name="foo[]" value="2"> 2
</label></div>
<div><label>
<input type="checkbox" name="foo[]" value="3"> 3
</label></div>
</form>
Example with errors:
var inputs = myForm.foo[];
Square bracket notation, on the other hand, allows:
var inputs = myForm["foo[]"];
Since the square brackets are part of a string, their special meaning doesn't apply.The second advantage of square bracket notation is when dealing with variable property names.
for (var i = 0; i < 10; i++) {
doSomething(myForm["myControlNumber" + i]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With