I'm reading a book on JavaScript called Eloquent JavaScript. The following is a snippet from the book:
"The two main ways to access properties in JavaScript are with a dot and with square brackets. Both value.x and value[x] access a property on value—but not necessarily the same property. The difference is in how x is interpreted. When using a dot, the word after the dot is the literal name of the property. When using square brackets, the expression between the brackets is evaluated to get the property name. Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result, converted to a string, as the property name."
While playing around with this bit of information, I happened to try this bit of code. It returns the value at the first index of the array. Why so?
arr = [1,2,3];
len = "length";
console.log(arr[len]);
// Output is "3" as expected
arr = [1,2,3];
console.log(arr.length);
//Output is "3" as expected
arr = [1,2,3];
len = "length";
console.log(arr[length]);
//Output is "1"
//The output I was expecting was an error or at least the value "3" since JS is a flexible language.