I am learning now JS but get so often this co开发者_开发百科ncept of "initiating a variable". Could anyone give me a good answer? Cheers
var name = 'john';
is declaring a variable and assigning the value 'john' to it, aka initializing it.
Simply means giving it a value so it's ready to use.
It's the next step from simply "declaring a variable" which you'll see in languages such as C, C#, Java, etc., which is more "making a variable ready to use, but leaving it 'empty' for now".
declaring a variable with var makes it local to the function it's in. Otherwise it could be available in other functions which can be confusing if you use the same variable name in other functions without declaring them with var...
精彩评论