Why is "var" needed before declaring a function when it still works without the "var"


#1


//This is the original example
var divideByThree = function (number) {
    var val = number / 3;
    console.log(val);
};
divideByThree(6);

//Without the var, the function still works.

divideByThree = function (number) {
    val = number / 3;
    console.log(val);
};
divideByThree(6);

When there is no var, the function still works. Could anyone explain why var should be needed in the function?

Thanks in advance!


#2

Yes ,It creates a huge difference
This thread will clear all your doubts.
Functions are stored inside variables too
just check this out..



#3

Wow, this definitely clears out my doubts. Thanks for sharing such a valuable information!


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.