Iterating through a javascript object to get key-value pairs

Here’s my code:

obj = {"TIME":123,"DATE":456}

console.log(obj.TIME);
console.log("---------")

for (var key in obj) {
  console.log(key);
  console.log(obj.key);
}

It prints as the following:

123
---------
TIME
undefined
DATE
undefined

Why does console.log(obj.key) print as undefined?

I want my code to print out the following, using obj.key to print out the value for each key:

123
---------
TIME
123
DATE
456

How do I do so?


Source: stackoverflow-javascript

Difference between [undefined × 2] and [undefined, undefined] in JavaScript

From https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/of,

Note: this implies an array of 7 empty slots, not slots with actual undefined values

What does it mean?

const foo = Array(2)
undefined
const bar = [undefined, undefined]
undefined
foo[0] === bar[0]
true
foo
[undefined × 2]
bar
[undefined, undefined]

What’s the difference between them? Is there any use case that I can only use one but not the other?


Source: stackoverflow-javascript