Which of the following is the correct way to define an object in JavaScript?

Which of the following is the correct way to define an object in JavaScript?

a) var obj = { key: value };
b) var obj = [ key, value ];
c) var obj = “key = value”;
d) var obj = (key, value);

Answer:

a) var obj = { key: value };

Explanation:

In JavaScript, objects are defined using curly braces {} and consist of key-value pairs. The keys represent properties, and the values can be any valid data type.

Example:


var person = {
    name: "John",
    age: 30,
    isStudent: false
};
console.log(person.name); // Output: "John"
    

Reference:

JavaScript MCQ (Multiple-Choice Questions)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top