why is 1[1] evaluated to undefined in javascript? [duplicate] - javascript

This question already has answers here:
Why is 0[0] syntactically valid?
(7 answers)
Closed 6 years ago.
Is the number 1 cast to an empty array implicitly?
> a = 1[1]
undefined
> console.log(a)
undefined
undefined

Is the number 1 cast to an empty array implicitly?
No. The number value is implicitly (and temporarily) converted to a number object and the property 1 is accessed on that object.
The bracket notation you are using ([1]) is not specific to arrays, every object property can be accessed via bracket notation. I.e. the following two are equivalent: obj.foo (dot notation) and obj['foo'] (bracket notation).
Number objects however don't have a property 1 and accessing a non-existing property returns undefined:
var numObj = new Number(1);
console.log(numObj[1]);
The implicit conversion happens every time you access a property on a primitive value, whether the property exists or not. In the following example, the primitive number value is also implicitly converted to an object and then its toFixed method is executed:
var num = 42;
console.log(num.toFixed(2));
// or equivalently to your example:
console.log(42['toFixed'](2))
Numbers, Strings, Booleans and Symbols have object equivalents. Null and Undefined don't.

Related

JavaScript property accessors wrapped in array [duplicate]

This question already has answers here:
Why does 2 == [2] in JavaScript?
(9 answers)
Closed 6 years ago.
I'm failing to find documentation as to how/why the following works:
const data = {one: 1, two: 2}
const key = ['one']
data[key[0]] // 1
data[key] // 1
data[[key]] // 1
data[[[key]]] // 1
data[[[[key]]]] // 1
data[['one', 'two']] // undefined
What allows any number of square brackets to surround the key and still successfully find the key in the object? Is there specific behavior when array.length === 1? And if so, where can I find documentation or clarity on that?
When using the object[key] bracket notation to access properties, the key is converted to a string.* The string representation of an array is the string representations of its elements joined with ,. For a one-element array, that's the same as the string representation of its only element.
const foo = {'a,b': 2};
console.log(foo[['a', 'b']]); // 2;
* Or a symbol.
When you access a property using square brackets, the key is always converted to string. And if you convert an array to string, the result is the same as calling the join() method on the array. Therefore, converting to string an array, which contains only one element, which is a string results in that element itself. However, when array contains two string elements (like ['one', 'two']), converting it to string results in 'one,two', and the data variable doesn't contain that key.

JS – Object literal converted to number? [duplicate]

This question already has an answer here:
Why is {}+[] different from ({}+[])? [duplicate]
(1 answer)
Closed 6 years ago.
While playing around in Chrome's dev console, I noticed something that I don't understand:
({})+"" evaluates to "[object Object]", as I expected
{}+"" evaluates to 0 (number)
Replacing {} with {foo:"bar"} in either expression doesn't change the result, so it does seem to be being parsed as an object literal. Replacing "" with "42" yields 42, and replacing it with "foo" yields NaN.
What's going on here?
The context of the grammar changes.
({}) is an object literal expression evaluating to a new object so the code is effectively anObject+"". The parenthesis are defining here as they cause the code to be parsed as "(..some expression..)".
{}+"" is parsed as {};+"" since the {} are parsed as an empty block (as opposed to an object literal). This makes the code equivalent to +"" which yields 0.
{foo:"bar"} is parsed as block, with a label ("foo") to the singular expression "bar". There is no object literal.
Some (useful) ways where {..} will be parsed as an object literal expression:
z = {..}
({..})
f({..})
return {..}

Difference between value.x and value[x] (getting property from array) in Javascript? [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 7 years ago.
I'm reading this part of Eloquent Javascript, and got stuck with the properties.
It said that
Both value.x and value[x] access a property on value—but not necessarily the same property.
......
Whereas value.x fetches the property of value named “x”, value[x] tries to evaluate the expression x and uses the result as the property name.
(value is an array and x is property like .length in this example.)
What's the difference between value.x and value[x]? I don't know how value[x] evaluate expression.
I don't know how value[x] evaluate expression.
To evaluate an expression means to substitute all the symbols with corresponding values and apply all (if any) operations to them (and JS does everything for you so you should not think of it any hard ever).
In case of as trivial expression as just x - its evaluation equals to the value the variable x refers to.
Eg:
var x = 'vvv';
alert(value[x]);
In this example the vvv property of the value object will be alerted.

what is a difference myObj.a=b vs myObj[a]=b [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed 8 years ago.
I was reading here on stack overflow that these are not equal. So what is the difference.
What happened is that in 2nd case value was assigned as the property of myObj. So if b='abc';
then myObj.abc was now available.
I had always thought same thing but [] version was used when name were weird ones.
Dot notation takes an identifier that is the property name. The square bracket notation accepts a string representation of the property name.
Given var a = "a"; then myObj.a = b and myObj[a] = b and myObj["a"] = b are equivalent.
The difference between myObj.a=b and myObj[a]=b is that in the first case you are accessing an attribute called a in the object. In the second you are accessing an attribute whose name is in a variable called a.
On the other hand, myObj.a=b and myObj["a"]=b would be equivalent.
a lot, results would depends on a var value. but ["a"] would be the same as .a

check object Equal object javascript [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do you determine equality for two JavaScript objects?
I want to check if two objects not different :
var v1 = {id:"llll", dd="kkkk"};
var v2 = {id:"llll", dd="kkkk"};
if (v1 == v2)
{
alert("lll");
}
not work why ????
Because objects are compared by reference:
Functions
Objects (Host objects, native objects/constructors and instances)
A common instance is {}, which is similar to new Object.
The following object types are compared by value, not by reference:
Strings
Numbers
Booleans
null and undefined
Additionally, there's one object which is never equal to itself, not even by reference:
var test = NaN;
alert ( test == NaN ); // false
alert ( test == test ); // false (!)
To check whether two objects are equal, you have to define equality:
"Two objects are equal if they contain the same property names and values"
Which implies that object A has to have the same number of properties as object B, and that every property in A has also to be a property of B.
Try using "===" instead of "==" to compare the objects.

Categories

Resources