Delete statement with var and non var

I tried deleting the variable in javascript using the delete statement, but found some problem. Can you guys explain the code below and why this is happening.

>> var a = 5;

>> delete a
false

>>a
5

>> b=5;

>>delete b
true

>>b
ReferenceError b is not defined

why are var a = 5 and b = 5 different?

+4
source share
4 answers

When a variable is created using a variable declaration (i.e. using var), it is created with the checkbox selected to remove false.

When a variable is created implicitly by assignment without declaration, its delete flag is set to true.

, ( eval-). , :

var a;

a - , ( ) delete, false. :

a = 'foo';

, true.

, , ( ).

+6

var = , (window ).

delete , .

+6

delete , . , var.

delete objectName.property 
delete objectName[index] 
+3

delete , . .

: -

var x = 5, x , . , , .

x = 5, . x . x , , x .

+1

Source: https://habr.com/ru/post/1531104/


All Articles