Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please explain the rationale behind this simple javascript interview question

Tags:

javascript

Consider this program:

(function(x){
  delete x;
  return x;
})(1);

What is the output?

It happens to be 1 and not undefined as I expected. I thought x purely is in the scope of function and deleting it inside the function should have made it inaccessible to outside it.

Can someone throw a little light on this?

like image 272
Eastern Monk Avatar asked Dec 21 '22 10:12

Eastern Monk


1 Answers

See the manual for delete:

The delete operator deletes a property of an object

and

If expression does not evaluate to a property, delete does nothing.

A variable is not a property.

like image 124
Quentin Avatar answered Dec 24 '22 00:12

Quentin