0
var a = {name:"John", lastName:"Willow"};

console.log(a.name + " " + a.lastName);

console.log(a["name"] + " " + a["lastName"]);

When do I have to use a["name"] - calling method with bracket instead of a.name ? I read some on the net and I don't understand the difference between these two calling methods.

please help!

David Tansey
  • 5,813
  • 4
  • 35
  • 51
  • 1
    Brackets are more general. The dot can only be used if the name is a proper identifier. – Hunan Rostomyan Nov 26 '15 at 01:29
  • the answers thus far seem to reference the difference in usage.. a more reasonable answer is to relate the concept of Arrays and Objects in Javascripts notorious weak-type interpreter, and how using an array method on a non-Array object resolves.. – Brett Caswell Nov 26 '15 at 01:42
  • @BrettCaswell: `[]` is not a method in JavaScript, it is not at all specific to Arrays in JavaScript, and is one of two syntaxes available for [property access on objects](https://es5.github.io/#x11.2.1). Array elements in JavaScript are (a special kind of) object properties, and behave like object properties in most respects (even though their underlying implementation is often quite different). You can access object elements using `[]`, as shown, and the only reason why one couldn't access array elements using the dot notation is the fact that array indices are not valid identifiers. – Amadan Nov 26 '15 at 01:56
  • @Amadan `[]` is generally referred to as an **indexer** or **index operator**, it is as much a 'method' as any other operator (I'm not sure why I said method here, I thinking Array.prototypes I suppose); however, it **IS** the literal constructor to `Array` in Javascript (i.e. `var obj = []`); It is common to use the literal constructor to access the prototype methods of Array `[].splice(x, y)`. in OP's usage though, it is an indexer, and furthermore, it iterates over Enumerable properties only, so there is conditional aspects to usage, they are not equivalent. – Brett Caswell Nov 26 '15 at 02:41
  • @BrettCaswell: You did not read my link, then. **In JavaScript**, `[]` in `obj[]` is a property accessor. Not an indexer, not an index operator, except colloquially. In `[].splice`, it is not even that, but another construct altogether, an [array initializer](https://es5.github.io/#x11.1.4), which has no bearing on the name or function of the bracket-notation property accessor. Neither of them iterate over anything; `for-in` statement and `Array.prototype.forEach` and similar functions do, and Enumerableness of properties only has to do with `for-in`, not with arrays. So much misinformation... – Amadan Nov 26 '15 at 03:34

2 Answers2

0

Briefly speaking you use brackets to reference array items (name is the key). You use dots to reference object properties.

In your example you deal with object so using a.name and a.lastName is a good approach.

Matt Komarnicki
  • 5,198
  • 7
  • 40
  • 92
0

With the bracket notation, you can use any string as the key. With dot, you are limited to constants. Consider this contrived example:

var box = { x1: 20, y1: 10, x2: 30, y2: 50 }
function getCoordinate(axis, index) {
  return box[axis + index];
}
getCoordinate("x", 1);
// => 20

You cannot do this with the dot notation. Similarly, if the key contains any characters that cannot be in an identifier, you cannot use the dot notation (a["="] is okay, a.= is not).

However, when you can use constants, it is more comfortable and more readable to write a.name than a["name"] (They are otherwise equivalent).

Amadan
  • 191,408
  • 23
  • 240
  • 301