Q
What will the following code output? console.log(person['age']); if var person = {name: 'John'};

Answer & Solution

Answer: Option C
Solution:
Accessing a non-existing property of an object returns undefined.
Related Questions on Average

What is the purpose of the this keyword in a constructor function?

A). It refers to the new object being created.

B). It refers to the global object.

C). It refers to the current object.

D). It refers to the parent object.

How do you call a method from a parent class in a subclass in JavaScript ES6?

A). super.methodName()

B). this.methodName()

C). parent.methodName()

D). base.methodName()

What will console.log(car.make); output if var car = {make: 'Toyota', model: 'Corolla'};?

A). Toyota

B). Corolla

C). Undefined

D). Error

How do you iterate over all properties of an object var person = {name: 'John', age: 30};?

A). for (var key in person)

B). for (var key of person)

C). forEach(key in person)

D). forEach(key of person)

How do you add a new property gender with value male to the person object?

A). person.gender = 'male';

B). person.gender = male;

C). person.gender = ['male'];

D). person[gender] = 'male';

How do you add a method to an object created with a constructor function?

A). this.methodName = function() {}

B). this.methodName: function() {}

C). this.methodName = () {}

D). this.methodName: () {}

How do you inherit properties and methods from another class in JavaScript ES6?

A). class SubClass extends SuperClass {}

B). class SubClass inherits SuperClass {}

C). class SubClass implements SuperClass {}

D). class SubClass super SuperClass {}

What is the output of console.log(person.greet()); if var person = {greet: function() {return 'Hello';}};?

A). Hello

B). Undefined

C). Error

D). Hello

How can you delete the age property from the person object?

A). delete person.age;

B). remove person.age;

C). person.age = null;

D). person.age.delete;

How do you access the value of the name property in the object var person = {name: 'John', age: 30};?

A). person['name']

B). person.name

C). person->name

D). person::name