What is the difference between var x = 10 and x = 10 when entering the browser console?

In the browser console, if I type var x=10 , it shows undefined , and if I type x=10 , it shows 10 . Both perform the same task, but what's the difference? I do not ask the difference between using var and not using var?

+5
source share
6 answers

You are in the browser console, so you are alerady in the global scope and with or without var do not matter how the variable was saved:

To prove

However, = is an operator that returns the value that you assigned, so a = 1 will evaluate to 1, and you will see 2 when you enter b = 2 . var returns nothing, this expression is not an expression.

+3
source

EXPLANATION

case x = 10 :

This creates a variable in a global scope named x with a value of 10 . It is also an expression that returns 10 . This is useful in order to be able to do things like var x = y = 10; which sets both x and y to 10

case var x = 10 :

This creates a variable in the current scope, which is exactly what happens to the global scope named x with value 10 . Because it was created using the var syntax, it cannot be evaluated as an expression, so it returns undefined , which is printed to the console.

SUMMARY

There is no difference in writing var x = 10 vs x = 10 from the console, although it will be in other places. The latter is also prohibited in strict mode. However, the first returns undefined because there is no exit at run time, however the second returns 10 because x=10 is an expression.

Example

You can see what happens a little better if you use eval

 var output = eval('x = 10'); console.log(output) // 10 

vs

 var output = eval('var x = 10'); console.log(output) // undefined 
+2
source

Scenario 1:

See Basically, when you type x = 10 in the browser console, you get 10 because 10 is the int value returned by the browser console.

Scenario 2: And when you enter var x = 10 in the browser console, you get "undefined" because it displays the return value of each command, and the console does not return anything in this script.

And further for more help:

http://blog.modulus.io/absolute-beginners-guide-to-nodejs

+1
source

var x = 10; sets the value for the current area (inside the function as an example).

x = 10; sets the value for the global scope, so it is available everywhere.

0
source

If var is used inside a function or other non-global scope, then the variable is not a global variable. If var is not used before the variable name, you have created a global variable.

/

 / These are both globals var foo = 1; bar = 2; function() { var foo = 1; // Local bar = 2; // Global // Execute an anonymous function (function() { var wibble = 1; // Local foo = 2; // Inherits from scope above (creating a closure) moo = 3; // Global }()) } 

If you are not doing the assignment, you need to use var:

 var x; // Declare x 
0
source
 // they both define var x and set it value to 10,but they are diff about below // x is globally x = 10 // x is scopely var x = 10 

In a browser, env globally means that var is defined as an Object window property

But be careful using the first code, it can cause a global definition variable.

And if it is in strict mode , this will not work , because the implicitly defined global does not allow.

0
source

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


All Articles