Multiple and singular array names in JavaScript

I am very confused by the behavior of this code in Chrome 59.0.3071.115:

var names = ["Cat 1", "Cat 2"];
console.log(names);

Prints an array object, but

var name = ["Cat 1", "Cat 2"];
console.log(name);

Prints a string "Cat 1, Cat 2".

Why is this happening?

+4
source share
1 answer

nameis the predefined property getter / setter of the objectwindow and will always be a string data type. Therefore, when you assign to it, you do not assign a variable, but use a setter that converts the given value into a string.

+5
source

Source: https://habr.com/ru/post/1682016/


All Articles