JavaScript Name Indexes in Javascript - Array / Object?

I understand that when index names are used to push values in Javascript, they essentially work like objects. But what I don't understand is the following behaviour -

person = [];
person[0] = "Someone";
person["test"] = "SomeoneElse"

Inputting person on the console prints ["Someone"] and I could see no information about person.test. person.test does print SomeoneElse. However, if I go console.log(person), I get ["Someone", test: "SomeoneElse"]. Curious to check if this makes sense, I tried to create a structure like this one -

var experiment = ["Someone1", test1: "SomeoneElse1"]

and what I get is

Uncaught SyntaxError: Unexpected token

What am I missing?

Thanks in advance!

Answer:1

Typing person on the console prints ["Someone"].

Array.prototype.toString formats this output, and it only considers the "array values" of itself without other properties.

However, if I go console.log(person), I get ["Someone", test: "SomeoneElse"].

console.log outputs other information about the object, including own properties.

Uncaught SyntaxError: Unexpected token

Because that is bogus syntax; the array literal syntax doesn't allow keys, because array values aren't supposed to have keys. Arrays are a numerically indexed list of values. Merely by the fact that under the hood those lists are implemented using objects (because everything in Javascript is an object of some kind or another) are you able to set "non numeric keys" on the array. That doesn't mean you're using the array correctly though.

Also see Are JavaScript Array elements nothing more than Array object properties?

Answer:2

This is because an array in JavaScript is also an object itself, and objects can have properties. So it is perfectly valid to have an array with elements, but also have properties set on the array object.

The second example doesn't work because the [...,...,...] syntax is specifically for instantiating an array and its elements.

Answer:3

I am new with javascript. I want to display a message in the client side if a person press a button continuously. So I think it is better to use a javascript code to show an Alert message if someone ...

I am new with javascript. I want to display a message in the client side if a person press a button continuously. So I think it is better to use a javascript code to show an Alert message if someone ...

  1. alert next line
  2. alert next line javascript

Im new to angular and while doing a small exercise, i was struck and I wanted to enable the ng-show depending upon previous row timings and time input is through jquery timepicker, but my angular is ...

Im new to angular and while doing a small exercise, i was struck and I wanted to enable the ng-show depending upon previous row timings and time input is through jquery timepicker, but my angular is ...

  1. angular-jquery-timepicker
  2. angular-jquery-timepicker npm
  3. angularjs jquery timepicker
  4. angular jquery datepicker
  5. angular jquery datepicker example
  6. angular jquery datetimepicker
  7. angularjs jquery datepicker
  8. angularjs jquery datepicker not binding
  9. angularjs jquery datepicker not working
  10. angularjs jquery datetimepicker
  11. angularjs jquery datepicker directive
  12. jquery timepicker angular 2

am trying to do a ConditionExpression in a DynamoDB put to check whether a stored boolean is true (in this example, whether the user is already verified don't run the put), i'm using the javascript ...

am trying to do a ConditionExpression in a DynamoDB put to check whether a stored boolean is true (in this example, whether the user is already verified don't run the put), i'm using the javascript ...

  1. dynamodb putitem conditionexpression example
  2. dynamodb putitem conditionexpression
  3. dynamodb putitem conditionexpression python
  4. dynamodb putitem conditionexpression java
  5. boto3 dynamodb put_item conditionexpression
  6. dynamodb put_item conditionexpression

I am currently trying to render plot in React using D3. To integrate both of them , i have used the following link: http://oli.me.uk/2015/09/09/d3-within-react-the-right-way/. I have also tried ...

I am currently trying to render plot in React using D3. To integrate both of them , i have used the following link: http://oli.me.uk/2015/09/09/d3-within-react-the-right-way/. I have also tried ...

  1. making chart.js responsive