JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – delete work on object create method object

JavaScript Quiz – delete work on object create method object

Delete object create method object

In, javascript object create method Object.create() creates a new object, with the help of existing object. As the prototype of

Syntax of Object create method

Object.create(proto, [propertiesObject])

For more detail about the javascript Object.create() click here.

And delete operator works on object and array type values that pass reference. delete operator removes the key and value from the object but from the array, it leaves undefined.

delete expression 

delete object.property
delete object['property']

For more detail about the javascript delete operator click here.

JavaScript Question

In this javascript quiz, find out does delete work on object create method object because this object created from object itself.

First, we create a variable developer using the const keyword and assign an object to it. Which only contains one key.

const developer = {
 name : "JS Startup"
}

Now, we create another variable dev using const and assign the object return from Object.create() method.

const dev = Object.create(developer);

In the next step, we delete the name key from object using delete operator.

delete dev.name;

Finally, we are checking the value of name key in dev object using console log.

console.log(dev.name);

Any idea what should be the output of the above statement ?

JavaScript Quiz Test

Here, is the final code snippet which you need to solve.
const developer = {
 name : "JS Startup"
}

const dev = Object.create(developer);
delete dev.name;

console.log(dev.name); // output => ?

Answer – “JS Startup

Click To Run Code

Explanation

Now, you know the answer is “JS Startup” But why, does the delete operator not work on dev object. Let me explain.

delete indeed removes a property from an object. But if the object inherits the property you’re trying to delete, rather than having its own property with that name, delete won’t work.

You’re essentially trying to remove something that doesn’t exist. The property you’re trying to delete is a property of the object’s prototype (or elsewhere in the object’s prototype chain), not the object that inherits it.

If you want to delete the key then you have to remove from the developer object also.

const developer = {
 name : "JS Startup"
}

const dev = Object.create(developer);
delete dev.name; // // It deletes the property in the newDog, but still the Dog property contains the name property so when you console.log(newDog.name) it prints Dog.name property.

console.log(dev.name); // output => "JS Startup"

delete developer.name;

console.log(newDog.name); // now it's deleted

At last, That’s why we get an answer “JS Startup“.

Let me know in the comment section, what you think about it.

I hope you understand the concept and logic behind it.

But wait, i have something more for you –

Check out our other javascript quiz – 

  1. JavaScript Quiz – does class typeof operator give class
  2. JavaScript Quiz – array map converts an element value with parseInt
  3. JS Quiz – does the splice method replace array element
  4. JS Quiz – is the console log function really override

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page

If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.

Leave a Reply

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

Back to top
0 Shares
Share via
Copy link