Question: 1 / 155

What will console.log(lydia) and console.log(sarah) output when using the Person constructor correctly and incorrectly?

Person {firstName: "Lydia", lastName: "Hallie"} and undefined

When using a constructor function in JavaScript to create objects, it is essential to understand how instances are created and initialized. In the context of the question, if the Person constructor is used correctly, creating an instance named 'lydia' will yield an object with the expected properties. The output for console.log(lydia) will show this object, specifically reflecting the attributes assigned during its creation, such as firstName and lastName.

If the constructor is not utilized correctly for the second instance with the name 'sarah', it might imply that 'sarah' was not initialized as an object instance of the Person constructor. In this case, logging 'sarah' would result in 'undefined' since it was never assigned a value. Therefore, you would see the object created for 'lydia' output correctly, while 'sarah' appears as undefined, aligning precisely with the output specified in the selected answer choice.

This highlights the importance of using the 'new' keyword when instantiating objects from constructors in JavaScript, as failing to do so can lead to errors in how variables are defined or initialized.

Person {firstName: "Lydia", lastName: "Hallie"} and Person {firstName: "Sarah", lastName: "Smith"}

Person {firstName: "Lydia", lastName: "Hallie"} and {}

Person {firstName: "Lydia", lastName: "Hallie"} and ReferenceError

Next

Report this question