Code Lab

Object keys and indexes

EasyBasics5 min

What does this program print? Write the output: one line for each console log call, in the same order.

const answers = { true: 'yes', 1: 'no', 1.0: 'maybe' };
console.log(answers);
console.log(Object.keys(answers).length);
console.log(['123', 'yes'][0][1]);
console.log(['123', 'yes'][false]);
  • Think about what each key of the object becomes. JavaScript changes values to other types by itself (type coercion).
  • In the last two lines, look at what each index picks.
  • Type each line exactly as the console shows it. An object is printed like { a: 1 }, with spaces inside the braces, and a string inside an object has single quotes. A key that is a valid variable name has no quotes; any other key, for example one that starts with a digit, is printed in single quotes.

Program

const answers = { true: 'yes', 1: 'no', 1.0: 'maybe' };
console.log(answers);
console.log(Object.keys(answers).length);
console.log(['123', 'yes'][0][1]);
console.log(['123', 'yes'][false]);

Asked at Rostelecom · Source

Predict the output
Loading the editor...
Run your code to see the test results here.