What is "var" used for in JavaScript?


The title pretty much explains it. I see it come up a lot here but I do not know what it means, what it does or if it even matters whether or not you use it. Thanks!


Check out this link

about JavaScript variables


There are two ways to declare a variable in javascript:

car = "volvo";
var car = "volvo";

they might seem the same, but they are not. There scope is different, when using the var keyword, the variable is not accessible outside out the function, as you can see in @rydan example, the variable which declared with keyword var is not accessible outside function, while the variable without keyword var is accessible outside function:

function x() {
  y = 1;   // Throws a ReferenceError in strict mode
  var z = 2;


console.log(y); // logs "1" 
console.log(z); // Throws a ReferenceError: z is not defined outside x

so they might seem the same, when it comes to scoping they are very different.