in js we must first declare a variable then we can use it but when I wrote these two lines of code, it works Properly!!
name = 'ali';
console.log(name);
in js we must first declare a variable then we can use it but when I wrote these two lines of code, it works Properly!!
name = 'ali';
console.log(name);
It's recommended that you declare variables, but it's not needed (unless you are in strict mode)
From a technical point of view, with the example you gave is also not a variable declaration per se, but you're setting a property of the global object.
There's a temptation to think that all of the code you see in a JavaScript program is interpreted line-by-line, top-down in order, as the program executes. While that is substantially true, there's one part of that assumption that can lead to incorrect thinking about your program.
When you see name = 'ali';, you probably think of that as one statement. But JavaScript actually thinks of it as two statements: var name; and name = ali;. The first statement, the declaration, is processed during the compilation phase. The second statement, the assignment, is left in place for the execution phase.
So your snippet then should be thought of as being handled like this:
var name;
name = 'ali';
console.log(name);
So, one way of thinking, sort of metaphorically, about this process, is that variable and function declarations are "moved" from where they appear in the flow of the code to the top of the code. This gives rise to the name "Hoisting".
In this particular case, because you are testing this concept with an identifier named name
, the actual answer is different than the others posted here, which would be correct if the identifier name was not a property of the Global window
object, but in your case name
is a property of window
.
So, what is happening here is that when you assign a value to name
, the system begins searching the scope chain for an identifier named name
and if it can't find it along the scope chain, it checks the Global scope, which in a browser is where window
is. Since the window
object does have a name
property, all that happens in your example is that you are setting a value for it (it defaults to an empty string). And, then you log that property.
If your question used a different identifier name - - one that was not the name of a Global object property, then because it has no declaration keyword, it would be effectively act like a Global variable because again, it would try to find that name along the scope chain, not find it and instead find the Global object (window
). Then, it would create a new property on that object and you'd have a new Global property to use.
A couple of examples:
// Test to see if there is a Global name property:
console.log(!!window.name); // true
// Test to see if there is a Global Scott property:
console.log(!!window.Scott); // false
// Create a Global Scott property
Scott = "Marcus";
// Test to see if there is a Global Scott property now:
console.log(!!window.Scott, window.Scott); // true "Marcus"