I\'m curious to know the difference between declaring a variable, and initializing a variable. e.g.
var example; // this is declaring
var example = \"hi\" /
There is a small thing that you are missing here. An analogy to understand the concept is like this. For each variable there has to be some value assigned to it.
The default value for all the variables (if not explicitly mentioned is undefined)
1) let example; // this is declaring and initializing with undefined
2) example="hi"; // this is assigning the value to hi
3) let example = "hi" // this is declaring and initializing with "hi"
so the 3rd statement is effectively the same as 1+2.
Now, a question may arise that when statement 3rd is possible, why do we need statement 1?
The reason is to expand the scope of variable.
e.g. let us say that a variable is required at line number 8. But the value is not available until late and that too in a code block.
1) {
2) let a;
3) try{
4) a=someFunctionWhichMayThroeException();
5) }
6) catch(e){
7) a=100;
8) }
9) someFunctionOnA(a);// the variable is required here
10)
11) }
By declaring the variable above, we have increased the scope of the variable hence it can be used outside the try block.
PS: this is just a trivial example of the usage.