Correct Answer: D
1. Getter Functions in JavaScript
In JavaScript, when an object uses the get keyword, it defines a getter method. Accessing a getter property executes the function and returns its value. Thus:
o.js
does not return the getter function; instead, it executes the function located at:
get js() { ... }
and returns the object inside the return block.
2. Behavior of String() and toLowerCase()
Inside the getter:
let city1 = String('St. Louis');
let city2 = String('New York');
String() creates a string value.
Then, the returned object is constructed as:
{
firstCity: city1.toLowerCase(),
secondCity: city2.toLowerCase(),
}
The method toLowerCase() is a standard JavaScript string method that returns a new string with all alphabetic characters converted to lowercase.
Therefore:
city2.toLowerCase()
returns:
'new york'
3. Referencing the Property
When the developer writes:
o.js.secondCity
the following happens:
The getter js runs and returns an object.
The returned object includes the property:
secondCity: 'new york'
Accessing .secondCity retrieves the lowercase string 'new york'.
Therefore, the correct value is 'new york'.
Why the Other Options Are Incorrect
A . undefined - incorrect because the property secondCity clearly exists in the returned object.
B . An error - incorrect because no invalid operations occur; all methods and properties are valid.
C . 'New York' - incorrect because toLowerCase() transforms the string to lowercase.
JavaScript Knowledge Reference (Text-Based)
Getter methods using the get keyword return computed values when accessed.
JavaScript String values support the toLowerCase() method, which returns a lowercase version of the original string.
Accessing nested properties like o.js.secondCity triggers the getter, returning the constructed object.