JavaScript Object Fundamentals Quiz Quiz

Sharpen your understanding of JavaScript objects with these easy questions, ideal for beginners and anyone reviewing the basics. Each question covers a different foundational aspect of JavaScript objects.

  1. JavaScript Object Syntax

    Which of the following correctly defines a JavaScript object with properties 'name' and 'age'?

    1. var person = { name: 'Alice', age: 25 };
    2. var person = [ name: 'Alice', age: 25 ];
    3. var person = 'name: Alice, age: 25';
    4. var person = ( name: 'Alice', age: 25 );

    Explanation: JavaScript objects are defined using curly braces and key-value pairs, as in the first option. Square brackets are for arrays, parentheses are not used for objects, and the last option defines a string, not an object.

  2. Accessing Object Properties

    Given var car = { model: 'Toyota', year: 2020 }, which code accesses the value 'Toyota'?

    1. car.model
    2. car['model']
    3. car-model
    4. car(model)

    Explanation: Object properties can be accessed with dot notation (car.model). Bracket notation is also valid, but 'car.model' is direct and correct. 'car(model)' is a function call, and 'car-model' is invalid syntax.

  3. Adding Properties to Objects

    How can you add a new property 'color' with value 'red' to an existing object bike?

    1. bike = { color: 'red' };
    2. bike.color = 'red';
    3. color.bike = 'red';
    4. bike('color') = 'red';

    Explanation: Adding a property is done with dot notation as in the correct answer. The second option reverses the object/property relationship. The third is not valid JavaScript, and the fourth option replaces the whole object instead of adding a property.

  4. Object Property Types

    Which type of values can a JavaScript object property NOT hold?

    1. A number
    2. A function
    3. An undefined value
    4. JavaScript object properties can hold any type

    Explanation: JavaScript object properties are flexible and can hold any data type, including numbers, undefined, and even functions. Therefore, there is no type that they cannot hold.

  5. Deleting an Object Property

    What JavaScript keyword removes a property from an object?

    1. unset
    2. remove
    3. delete
    4. discard

    Explanation: The 'delete' keyword is the correct way to remove a property from a JavaScript object. The other options are not valid JavaScript keywords for this action.

  6. Checking Property Existence

    Which syntax checks if an object user has a property named 'email'?

    1. user->email
    2. user.has('email')
    3. 'email' in user
    4. email in user

    Explanation: The 'in' operator checks if a property exists in an object. The second and third options are not valid JavaScript, and the fourth option has the operands reversed.

  7. Iterating Properties

    Which loop iterates over all enumerable properties of a JavaScript object?

    1. for (key of object)
    2. foreach key in object
    3. forEach(object)
    4. for (var key in object)

    Explanation: The 'for...in' loop is used to iterate over object properties. 'forEach' works with arrays, 'for...of' is designed for iterable objects like arrays, and 'foreach key in object' is not valid syntax.

  8. Object Methods

    How do you define a function inside a JavaScript object so it becomes a method?

    1. As a number value
    2. As a string value
    3. You cannot define functions in objects
    4. As a property with a function value

    Explanation: A method is a property whose value is a function. Defining it as a string or number won't make it a method, and JavaScript does allow defining functions inside objects.

  9. Nested Objects

    Given an object student with a property address that is also an object, how do you access the city inside address?

    1. student.address.city
    2. student(city)
    3. student.address->city
    4. student['address'.city]

    Explanation: You access nested object properties with dot notation as in 'student.address.city'. The other options use incorrect syntax for accessing nested properties.

  10. Object Comparison

    What happens if you compare two different objects in JavaScript that have identical properties and values using '=='?

    1. They are equal only if they have the same properties
    2. They are always equal
    3. JavaScript throws an error
    4. They are not equal

    Explanation: In JavaScript, objects are compared by reference, not by their content. Two separately created objects with identical properties and values are still considered different, so the comparison returns false. The other options misrepresent how JavaScript handles object comparison or suggest errors that do not occur.