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 be the outcome of trying to access properties on an object created without new?

  1. It treats this as an instance of global object.

  2. It fails and throws a ReferenceError.

  3. It initializes an empty object.

  4. It throws a SyntaxError.

The correct answer is: It treats this as an instance of global object.

When you create an object without using the `new` keyword in JavaScript, the behavior is that the function is treated as if it's being called in the context of the global object. This means that if the function being called does not explicitly return an object, the properties you attempt to access will refer to the global object. In a non-strict mode context, if you call a constructor function without `new`, the `this` keyword inside that function refers to the global object. Thus, accessing any properties defined within that function will modify or reference properties on the global object rather than creating a new instance of the intended object. This behavior enables the properties to be accessed just like any global variable. In strict mode, however, it would yield `undefined` for `this`, resulting in a more restrictive behavior, but the initial question does not indicate that strict mode is being used, and often, JavaScript code defaults to non-strict mode. Other choices indicate various types of errors or outcomes that wouldn't occur in this scenario. For instance, there would not be a ReferenceError for attempting to access properties because the object still exists at the global level, nor would it throw a SyntaxError as that's related to incorrect code syntax rather than runtime behavior.