Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0613· OpenAIHugging Face Arena Human Preference 55K

How to access a property of a Javascript object using a text string that is the name of…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with gpt-4-0125-preview’s answer.
  1. User

    How to access a property of a Javascript object using a text string that is the name of the property ?

  2. Assistant

    You can access a property of a JavaScript object using a text string that is the name of the property by using bracket notation. Here is a simple example:

    var obj = {
      name: "John Doe",
      age: 30
    };
    
    var propertyName = "name";
    
    console.log(obj[propertyName]);  // Outputs: "John Doe"
    

    In this example, propertyName is a string with a value of "name". When we use obj[propertyName], JavaScript interprets this as wanting to access the property with the name equivalent to the value of propertyName, which in this case is "name". This allows us to dynamically access properties on an object.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.