JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – can delete operator delete a global variable

JavaScript Quiz – can delete operator delete a global variable

delete operator on global variable –

Javascript delete operator removes a property from an object like array and object. But when delete operator used on global variable without var, let & const it behave different.

As array and object are non-primitive type so delete operator work on it but not work on primitive type

It’s syntax looks like –

delete expression

For more detail about delete operator click here.

JavaScript Question

In this javascript quiz code, you need to check whether the delete operator work on a global variable if yes then why because it’s not object

First, we declare a variable first using let keyword and assign an “JS” string to it. We also check their value using console.log;

let first = “JS”;

console.log(first); // output => ‘JS’

In the second step, we use the delete operator on the variable first then check it’s value. But it values is still “JS“;

delete first;

console.log(first); // output => ‘JS’

Now, create another global variable without var, let & const and assign a javascript string “Startup” to it. We also check it’s value using a console log.

let last = “Startup”;

console.log(last); // output => ‘Startup’

Finally, we are using the delete operator on a global variable last, and let’s find out what is the value after it.

delete last;

console.log(last); // output => ?

Any idea what should be the output of it ?

Answer – Error

Click To Run Code


Now, you know the answer is “Error“. But why as previously stated that delete operator not work on primitive type. Let me explain

It’s correct that delete operator not work on non-primitive type that’s why the value of first variable not change.

But you know when you declare a variable without var, let, and const keyword. It can be accessed globally because of a window or this object.

console.log(this.last); // output => ‘Startup’

Ans this is an object. So, after the delete operator, you will get the following error “ReferenceError: last is not defined.

Note: I know var keyword value also accessible by this object but as variable created with var keyword so delete operator not work on it.

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

Let me know in the comment section, any alternative of 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 typeof of Symbol really give a string
  2. JavaScript Quiz – can setTimeout in the for loop print values
  3. JS Quiz – can reduceRight method really sum an array elements
  4. JS Quiz – includes method can really remove duplicates with forEach


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