Prepare for the Salesforce JavaScript Developer Exam. Utilize comprehensive quizzes, flashcards, and multiple choice questions with hints and explanations. Boost your exam readiness!

Practice this question and more.


What will happen when referencing a non-existent property of an object in JavaScript?

  1. It will throw a TypeError

  2. It will return undefined

  3. It will throw a ReferenceError

  4. It will create the property dynamically

The correct answer is: It will return undefined

When referencing a non-existent property of an object in JavaScript, the behavior is to return undefined. This means that if you attempt to access a property that does not exist on an object, JavaScript does not throw an error. Instead, it evaluates the expression and returns a value of undefined. This behavior is part of JavaScript's design, which allows for flexible handling of objects and their properties. For example, if you have an object defined as `let obj = { a: 1 };` and you try to access `obj.b`, JavaScript will return undefined because the property `b` is not defined within the object `obj`. This approach allows developers to check for the existence of a property without needing to handle exceptions actively, streamlining the process of working with objects in dynamic contexts. Other options imply error scenarios or behaviors that do not align with how JavaScript handles missing object properties. Specifically, TypeErrors and ReferenceErrors are thrown in circumstances involving improper access to object states or attempting to use non-existent variables rather than properties, and dynamic property creation is not the behavior of object property access when the property isn't defined in the first place.