JavaScript Interview Question #48: Dog-sized Cat

How many errors are in this code snippet? What’s the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In JavaScript there are two ways to get access to the object properties.

using the dot

const dog = { name: ‘Rex’, age: 2, size: ‘…


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer

javascript interview question #48

How many errors are in this code snippet? What’s the output?

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

In JavaScript there are two ways to get access to the object properties.

  • using the dot
  const dog = { name: 'Rex', age: 2, size: 'big' };

  console.log(dog.name); // Rex
  console.log(dog.age);  // 2
  console.log(dog.size); // big
  • using square brackets
  const cat = { name: 'Tom', age: 5, big: false };

  console.log(cat['name']); // Tom
  console.log(cat['age']);  // 5
  console.log(cat['big']);  // false

Notice, that we’ve used field names as plain strings inside of the square brackets.

If we go to the original question, then the statement

console.log(cat[dog.size]);

Is the same as

console.log(cat['big']);

Which is equivalent to

console.log(cat.big);

In all three cases, we get access to the field big of the object cat.

ANSWER: There are no errors in the code snippet. The value false appears in the console when the code is executed.

Learn Full-Stack JavaScript


This content originally appeared on DEV Community and was authored by Coderslang: Become a Software Engineer


Print Share Comment Cite Upload Translate Updates
APA

Coderslang: Become a Software Engineer | Sciencx (2021-06-18T13:06:31+00:00) JavaScript Interview Question #48: Dog-sized Cat. Retrieved from https://www.scien.cx/2021/06/18/javascript-interview-question-48-dog-sized-cat/

MLA
" » JavaScript Interview Question #48: Dog-sized Cat." Coderslang: Become a Software Engineer | Sciencx - Friday June 18, 2021, https://www.scien.cx/2021/06/18/javascript-interview-question-48-dog-sized-cat/
HARVARD
Coderslang: Become a Software Engineer | Sciencx Friday June 18, 2021 » JavaScript Interview Question #48: Dog-sized Cat., viewed ,<https://www.scien.cx/2021/06/18/javascript-interview-question-48-dog-sized-cat/>
VANCOUVER
Coderslang: Become a Software Engineer | Sciencx - » JavaScript Interview Question #48: Dog-sized Cat. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/18/javascript-interview-question-48-dog-sized-cat/
CHICAGO
" » JavaScript Interview Question #48: Dog-sized Cat." Coderslang: Become a Software Engineer | Sciencx - Accessed . https://www.scien.cx/2021/06/18/javascript-interview-question-48-dog-sized-cat/
IEEE
" » JavaScript Interview Question #48: Dog-sized Cat." Coderslang: Become a Software Engineer | Sciencx [Online]. Available: https://www.scien.cx/2021/06/18/javascript-interview-question-48-dog-sized-cat/. [Accessed: ]
rf:citation
» JavaScript Interview Question #48: Dog-sized Cat | Coderslang: Become a Software Engineer | Sciencx | https://www.scien.cx/2021/06/18/javascript-interview-question-48-dog-sized-cat/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.