Randomblue
Randomblue

Reputation: 116333

Using JavaScript console modifies outcome of script

In Chrome, this fiddle prints false. However, the same code typed in the JavaScript console prints true:

a = 1;
var a = 2;
console.log(delete a);​

Why do I get different results depending on whether or not I'm using the Chrome console?

Upvotes: 3

Views: 85

Answers (2)

yapingchen
yapingchen

Reputation: 856

var defined variables can not be deleted.

Upvotes: 0

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

Because the Chrome console runs inside an eval construct or something similar, rather than running in the global scope.

There is a lot of discussion about the delete operator here on StackOverflow. A search for [javascript] delete will help answer other questions that come up.

Upvotes: 1

Related Questions