JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – Does delete operator really work on the primitives

JavaScript Quiz – Does delete operator really work on the primitives

Question –

In this quiz, you will need to find out the output of the value variable. which is declared using let keyword and IIFE return value assigned to it after delete operator.

First, we declare a variable value and assign IIFE (Immediately Invoked Function Expression) function which at the same moment as it declare.

Here, we are passing parameter to IIFE function which is “jsstartup.com“.

And inside the IIFE function, we use the delete keyword and used it on name variable which currently has a “jsstartup.com”.

After using a javascript delete on name variable. We return a name variable.

Return value from IIFE function is going to be assign to value variable.

From now, interesting part begin what really IIFE function return?

Does delete operator really work on the primitives?

Answer –jsstartup.com

Click To Run code

Explanation –

Now, why the answer is still “jsstartup.com“. Doesn’t delete remove the value. It should be undefined or null.

Let’s first go through this two point –

IIFE (Immediately Invoked Function Expression) –

A function can be a named one or anonymous function (does not have a name) which can be called whenever you want. But, the IIFE function is a special one because it’s going to be executed as it declared or mounted.

For more detail on JavaScript IIFE Function

delete operator –

The JavaScript delete operator removes a property from an object. So if property or key remove from an object it values also removed.

delete work on object and array too. But in array case, index still there but value set to undefined.

For more detail on javascript delete operator

So, I hope you understand now. Why we are getting an answer “jsstartup.com“.

It’s because javascript delete operator work on array & object which are non-primitives type.

But, in our challenge name variable is just a primitive type. So, the delete doesn’t have any impact on the “jsstartup.com” value.

Check out our other javascript quiz – 

  1. FIND OUT THE SIZE OF A JAVASCRIPT MAP OBJECT
  2. DOES NESTED ARRAY REFERENCE REMOVED BY SPREAD SYNTAX

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
8 Shares
Share via
Copy link