A locally scoped variable is a variable declared with the keyword var within a function. Local refers to the context of the function itself. When you declare a variable within a function it is made available throughout the entire function, and it is completely unavailable outside of the function. For example:

var x = 1; function hello() { alert(x); var y = 2; } hello(); alert(y);

function sendMessage(message) { alert(message); } sendMessage('hello, friend'); console.log(message);

function go() { var x = 2; function add3() { return x + 3; } return add3(); } alert(go());

If you run this code you will find that the functionhas access tobecause it is a global variable, but when we try to alertoutside of the function we get an Uncaught ReferenceError. That's becauseis only available within the scope it was created. It is also important to note that parameters passed into a function are scoped locally. So in the following example:we see the alert but get another Uncaught ReferenceError because thevariable is declared as a parameter of the functionand is only available from within the function itself. As JavaScript allows for nesting functions, a variable declared within a function is also available within its nested functions. Take this for example:You would never write this, but if you did you would find thatis available from within thefunction. But as you might now assume, a local variable declared within a nested function is not available to its parent function. Keep in mind that in JavaScript, unlike many other programming languages, there is no block level scope. Instead JavaScript uses function level scoping.