1

I'm trying to understand the difference between the Dot and the Square Bracket Notation. While going through various examples here on SO and on some other sites, I came across these two simple examples:

var obj = { "abc" : "hello" };
var x = "abc";
var y = obj[x];
console.log(y); //output - hello

var user = {
  name: "John Doe",
  age: 30
};
var key = prompt("Enter the property to modify","name or age");
var value = prompt("Enter new value for " + key);
user[key] = value;
alert("New " + key + ": " + user[key]);

The first example returns y to be undefined if in third line I replace the obj[x] with obj.x. Why not "hello"


But in the second example the expression user[key] can simply be replaced with user.key without any anomalous behavior (at-least to me). Now this confuses me as I recently learned that if we want to access properties by name stored in a variable, we use the [ ] Square bracket Notation.

CalvT
  • 3,123
  • 6
  • 37
  • 54
Mani Malhotra
  • 97
  • 1
  • 3
  • 12
  • 2
    Possible duplicate of [JavaScript property access: dot notation vs. brackets?](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – Shammel Lee Jun 07 '17 at 16:03
  • 1
    Look at the *whole* object, not just the property you modified. – Quentin Jun 07 '17 at 16:05
  • `obj.key` means the property `key` (literally) of `obj`. `obj[key]` means the property whose key is stored in variable `key` of `obj`. There’s a clear difference. Using `obj.key` overwrites the same property, so you can’t set a name and an age simultaneously. Just look at your object with `console.log(obj)`. `name` and `age` won’t have changed and you’ll have another meaningless property `key` which holds some number or some string. – Sebastian Simon Jun 07 '17 at 16:18
  • @Xufox thanks for taking time. This line really helped me- "obj.key means the property key (literally) of obj. obj[key] means the property whose key is stored in variable key of obj." Sometimes one overlooks very obvious things, unless someone guides and points them in the right way... so thanks a lot pal. – Mani Malhotra Jun 08 '17 at 10:28

2 Answers2

6

In dot notation, the name after the dot is the name of the property being referenced. So:

var foo = "bar";
var obj = { foo: 1, bar: 2 };

console.log(obj.foo) // = 1, since the "foo" property of obj is 1,
                     //      independent of the variable foo

However, in square-bracket notation, the name of the property being referenced is the value of whatever is in the square brackets:

var foo = "bar";
var obj = { foo: 1, bar: 2 };

console.log(obj[foo])   // = 2, since the value of the variable foo is "bar" and
                        //      the "bar" property of obj is 2

console.log(obj["foo"]) // = 1, since the value of the literal "foo" is "foo" and
                        //      the "foo" property of obj is 1

In other words, dot-notation obj.foo is always equivalent to obj["foo"], while obj[foo] depends on the value of the variable foo.


In the specific case of your question, note the differences between dot notation and square-bracket notation:

// with dot notation
var obj = { name: "John Doe", age: 30 };
var key = "age";
var value = 60;

obj.key = value; // referencing the literal property "key"
console.log(obj) // = { name: "John Doe", age: 30, key: 60 }


// with square bracket notation
var obj = { name: "John Doe", age: 30 };
var key = "age";
var value = 60;

obj[key] = value; // referencing property by the value of the key variable ("age")
console.log(obj)  // = { name: "John Doe", age: 60 }
Frxstrem
  • 38,761
  • 9
  • 79
  • 119
  • Hey Frxstrem, can't thank you enough, your examples and your explanation cleared all my doubts regarding this topic and it is such a relief now... :D Thanks a bunch... – Mani Malhotra Jun 08 '17 at 10:18
1

Accessing/Creation of properties from/in a JavaScript object can be done in two ways

  1. Using Dot notation
  2. Using Square Bracket notation

Whenever some property is not defined i.e. not present in the object and you try to access it, you will get undefined (obviously, because it's not there).

So, in the first example you are accessing a property and in the second example you are creating a property. Therefore, replacing the notation did not affect the code in the second example.

Pratyush Sharma
  • 257
  • 2
  • 7
  • This line really helped me, "Whenever some property is not defined i.e. not present in the object and you try to access it, you will get undefined" – Mani Malhotra Jun 08 '17 at 10:31