LearnFrenzy
Sign in Take a free test

Objects · Predict the Output · Question 1

Dot vs bracket with a variable Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What does the following JavaScript code print (console output, as in Node.js)?

main.js
const k = "age";
const person = { age: 30, k: 1 };
console.log(person.k, person[k], person["k"]);
  • A 30 30 1
  • B 1 1 1
  • C 30 30 30
  • D 1 30 1 Correct
Correct Answer: Option D

person.k and person["k"] read the property named "k"; person[k] uses the variable, so it reads "age".

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links