I believe you.
So you mean there are cases where this would be true? Any examples?
Here is what I think of 'var', correct me if I am wrong.
'var' doesn't as such declare a variable.
Yes, it does.
A couple of things you should know:
1. Declarations are processed before any code is executed. That means
that if you don't declare variables, they don't exist until the code
is excuted, a feature that can have great significance for functions:
bar(); // No error, bar() exists when the call is executed
foo(); // Error! foo doesn't exist until the next line is executed.
var foo = function(){}; // Function statement
function bar(){} // Function declaration
2. The use of var establishes the context for the variable. Any
variable used without var will be created as a property of the global
object for that context *when the code is executed*. Note that
undeclared variables with eval may not behave as you expect.
3. Because of 1., using var on a variable multiple times within the
same scope has no effect:
var x = 3;
alert(x); // => 3
var x;
alert(x); // => 3
You don't need 'var' keyword
to *declare* variables
Yes, you do. A variable without var isn't declared, it is created as
a property of the global object when the code executes. It is
equivalent (more or less) to:
window[variableName] = 'foo';
or put them in the symbol tree in javascript.
Not sure about the term "symbol tree".
It's just an indication to the scripting engine that 'please put this
variable in a scope which is local to this function. So saying:
var a = 10;
var a = 11;
doesn't actually create two variables or declare two variables but
refer to the same variable both the times which is 'a' which was
declared and defined using the statement 'a = 10'. Thus prepending the
variable name with 'var' separates it from the global namespace
(scope) (assuming we are not using 'with' in which case we would have
an additional scope).
Is this good enough?
No. The variable a is declared twice, so before any code is executed,
a variable a is created within the scope of the declaration. When the
code is executed, a is firstly assigned a value of 10, then
immediately afterward a value of 11. The declaration is processed
once, the assignment is processed each time the code is executed.
This feature of javascript is often used where feature testing is
required - it can be done once up front and not every time the
function is called, e.g.:
Method 1 - using a function declaration:
function getText(el){
if(typeof el.textContent == 'string') {
return el.textContent;
}
if(typeof el.innerText == 'string') {
return el.innerText;
}
}
Method 2 - using a function expression:
var getText = (function(){
var el = document.getElementsByTagName('script')[0];
if (typeof el.textContent == 'string') {
return function(el){return el.textContent;};
}
if (typeof el.innerText == 'string') {
return function(el){return el.innerText;};
}
})();
The second method should be more efficient as the feature test is only
carried out once, whereas in the first it is done every time getText
is called. Note that for a production system, the above should be
extended to support browsers that haven't implemented either
textContent or innerText - I didn't included the code here for the
sake of brevity.