Declaring Variables Without Var Keyword

Declaring variables without var keyword

No, there's no RAM benefit or anything like that.

What w3schools is talking about is something I call The Horror of Implicit Globals. Consider this function:

function foo() {
var variable1, variable2;

variable1 = 5;
varaible2 = 6;
return variable1 + variable2;
}

Seems simple enough, but it returns NaN, not 11, because of the typo on the varaible2 = 6; line. And it creates a global variable with the typo'd name:

function foo() {
var variable1, variable2;

variable1 = 5;
varaible2 = 6;
return variable1 + variable2;
}
console.log(foo()); // NaN
console.log(varaible2); // 6?!?!?!

Is using 'var' to declare variables optional?

They mean different things.
If you use var the variable is declared within the scope you are in (e.g. of the function). If you don't use var, the variable bubbles up through the layers of scope until it encounters a variable by the given name or the global object (window, if you are doing it in the browser), where it then attaches. It is then very similar to a global variable. However, it can still be deleted with delete (most likely by someone else's code who also failed to use var). If you use var in the global scope, the variable is truly global and cannot be deleted.

This is, in my opinion, one of the most dangerous issues with javascript, and should be deprecated, or at least raise warnings over warnings. The reason is, it's easy to forget var and have by accident a common variable name bound to the global object. This produces weird and difficult to debug behavior.

difference between var keyword and without var

If var keyword is used within a function or other non-global scope then that variable's scope is not global .

If var keyword is not used before a variable name, then that variable's scope is global .

difference between declaring variables with var vs without var in go

If you use the :=, the type of the variable is implied from the expression on the right of the sign. If you use =, no assumption is made and you need to specify the type yourself.

In this case, you should write it like this:

var pic [][]uint8 = make([][]uint8, dy)

but this is indeed better because shorter and as clear:

pic := make([][]uint8, dy)

Declare variable without var keyword and logical OR

The first example tries to assign to a property on the global object named my_var by reading the value from an identifier called my_var (OR an empty object). However, the identifier my_var is not defined at that point, so it fails.

In the second example, due to how javascript variable hoisting works, the my_var variable is already declared, when you read from it by assign to it.

Also have a look at this example:

a = a; // fails, undeclared identifier
a = 0;

With var keyword it will work!

b = b; // succeeds allthough identifier undeclared?!
var b = 0;

This is because variable hoisting will turn it into this:

var b; // declaration of b hoisted to the top of scope
b = b;
b = 0;

Declare variable without var keyword and logical OR

The first example tries to assign to a property on the global object named my_var by reading the value from an identifier called my_var (OR an empty object). However, the identifier my_var is not defined at that point, so it fails.

In the second example, due to how javascript variable hoisting works, the my_var variable is already declared, when you read from it by assign to it.

Also have a look at this example:

a = a; // fails, undeclared identifier
a = 0;

With var keyword it will work!

b = b; // succeeds allthough identifier undeclared?!
var b = 0;

This is because variable hoisting will turn it into this:

var b; // declaration of b hoisted to the top of scope
b = b;
b = 0;


Related Topics



Leave a reply



Submit