Quite a lot of people who use Javascript may wonder sometimes why there is a var keyword. Although we may not notice it since there barely will come a point in time where we may not notice it, there is a real purpose for the var keyword. If you place the var keyword beside a variable name inside a function, it will set that variable’s scope to local. Meaning, once the function finishes its task, that variable will become non-existent.

Why am I blogging this? For such a simple keyword. I made this post to remind me that after so many years of using Javascript, I now know what the purpose of the keyword var really is for. The purpose for using the var keyword on a variable is to make it locally scoped. This means that if you use that inside a function, the variable’s scope is gone once the function is finished processing. I never noticed any difference with using a var or not simply because there never came a point in time that I would be having problems with the values of a variable. Until now. When I had problems with the values on a single variable, I was confused why. I tried to add the var keyword thinking, who knows right? And it worked! That was the time I decided to look up the purpose of the var keyword in Google and found the answer.

I laugh at myself why I discovered this until now after so many years. But hey, better late than never right? At least now I know he he he :D.

Related Posts Plugin for WordPress, Blogger...