Q
What is the purpose of the 'constructor' method in a JavaScript class?

Answer & Solution

Answer: Option B
Solution:
The 'constructor' method in a JavaScript class is used to initialize the class properties when new instances of the class are created. It is automatically called when you create a new object from the class. Therefore, option B, 'To initialize class properties', is correct for explaining the purpose of the 'constructor' method in a JavaScript class.
Related Questions on Average

How can you add a new element to the end of a JavaScript array?

A). colors.push('blue');

B). colors.add('blue');

C). colors.insert('blue', colors.length);

D). colors[colors.length] = 'blue';

How can you prevent a JavaScript function from executing immediately?

A). Using async/await

B). Using the defer attribute in HTML script tag

C). Wrapping the function in parentheses

D). Using the setTimeout function

Which operator is used for strict inequality in JavaScript?

A). !==

B). !=

C). <>

D). /=

How do you concatenate strings in JavaScript?

A). Using the + operator

B). Using the - operator

C). Using the * operator

D). Using the / operator

How can you comment a single line of code in JavaScript?

A). // This is a comment

B). <!-- This is a comment -->

C). /* This is a comment */

D). ''' This is a comment '''

Which statement correctly declares a JavaScript module?

A). export function myFunc() {}

B). const myModule = function() {}

C). module.exports = myModule;

D). import myModule from './myModule.js';

Which statement correctly declares a JavaScript object?

A). let person = {name: 'John', age: 30};

B). const person = ('name' => 'John', 'age' => 30);

C). const person = ['name', 'John', 'age', 30];

D). var person = {name: 'John', age: 30};

What is the purpose of the 'break' statement in a JavaScript switch case?

A). To continue to the next case

B). To exit the switch statement

C). To restart the switch statement

D). To execute the default case

Which method is used to remove the last element from a JavaScript array?

A). colors.pop();

B). colors.removeLast();

C). colors.splice(-1, 1);

D). colors.deleteLast();

What is the purpose of the 'continue' statement in a JavaScript loop?

A). To exit the loop

B). To skip the current iteration

C). To restart the loop

D). To execute the loop body again