Q
Which of the following is a valid way to define a constant object in JavaScript?

Answer & Solution

Answer: Option A
Solution:
Both options A and B are valid ways to define a constant object in JavaScript.
Related Questions on Average

Which of the following will cause an error when used with a constant object in JavaScript?

A). A. Modifying a property using bracket notation

B). B. Adding a property using dot notation

C). C. Deleting a property

D). D. Reassigning the object reference

Which statement about constant objects in JavaScript is correct?

A). A. Constant objects can have their properties reassigned

B). B. Constant objects cannot have properties added or removed

C). C. Constant objects are only applicable to primitive values

D). D. Constant objects are mutable

How can you ensure that a constant object remains unchanged in JavaScript?

A). A. Use Object.preventExtensions()

B). B. Use Object.freeze() to freeze the object

C). C. Use const for each property

D). D. All of the above

What is the data type of a constant object in JavaScript?

A). A. Object

B). B. Array

C). C. Function

D). D. String

What does the const keyword do when used to declare an object in JavaScript?

A). A. Makes the object immutable

B). B. Prevents reassignment of the object reference

C). C. Freezes all properties of the object

D). D. All of the above

What is the result of the expression Object.keys(person).length for the following code snippet?

A). A. 1

B). B. 2

C). C. 3

D). D. Error

What happens if you try to modify a property of a frozen object in JavaScript using Object.freeze()?

A). A. The property is modified successfully

B). B. A TypeError is thrown

C). C. The property is deleted

D). D. The object becomes mutable

Which of the following will result in an error when using a constant object in JavaScript?

A). A. Modifying an existing property

B). B. Adding a new property

C). C. Reassigning the object reference

D). D. None, all operations are valid with constant objects

How can you change the properties of a constant object in JavaScript?

A). A. Use Object.freeze()

B). B. Use const for each property

C). C. Use the delete keyword to remove properties

D). D. Use dot notation or bracket notation

What is the output of the following code snippet?

A). A. { a: 1, b: 2, c: 3 }

B). B. { a: 1, b: 2 }

C). C. { c: 3 }

D). D. Error