0

Could someone clarify for me please... I've read that creating a variable using reserved word 'var' makes that variable public but how can that be if the variable was created inside a function:

$('#timeIn').timepicker({ 'scrollDefaultNow': true });
    $('#timeIn').on('change', function() {
        var numIn = $('#timeIn').timepicker(('getSecondsFromMidnight'));
        var inHours = {
            hours: (numIn/86400)*24,
            getter: function() {return this.hours;}
        };
        timeIn = $('#timeIn').val();
        inArray.push(timeIn);
        events.push(timeIn);
});

In this example the variables numIn & inHours are only known within that onChange method, correct? If that is the case what would the global declaration look like? The 'timeIn' is globally scoped but without manipulation I only get a string representation back. What are my options for getting a computable time back as return.

jbabey
  • 45,965
  • 12
  • 71
  • 94
MCGRAW
  • 777
  • 14
  • 37
  • 1
    See [JavaScript Variable Scope](http://stackoverflow.com/questions/500431/javascript-variable-scope) –  Sep 06 '12 at 17:33
  • @pst you are correct sir but part of I wanted most was a confirmed rebuttal of the reserve word 'var', when and where to use it and what can see it-but there is a ton of information on scope at your link – MCGRAW Sep 06 '12 at 17:38

3 Answers3

10

Using the word var within the function binds it to that function's scope.

Not using the word var makes it public in all functions and all scopes

AlanFoster
  • 8,156
  • 5
  • 35
  • 52
  • 3
    Clarification: not using `var`, binds the name to the _global object_, which in browsers is the `window` object, but in other environments (e.g. Node) might be different. – lanzz Sep 06 '12 at 17:16
  • Also, an interesting minor detail: `var` in "global scope" has the side-effect of making it non-`delete`'able .. (it is resolvable as a property, but treated as a variable) –  Sep 06 '12 at 17:21
  • @pst you can't say `delete window.someVar;`? – jbabey Sep 06 '12 at 17:22
  • @jbabey `var x = 2; delete x; window.x` this differs from `y = 2; delete y; window.y` (assuming global scope in both examples) .. it's just a little detail, but `var` in global scope does do something. (My preference/style dictates explicitly using `window.globalProp` and not using `var` in a global context, but that's another story ..) –  Sep 06 '12 at 17:22
5

JavaScript uses function scope - every variable can be seen only from within the same function or a scope higher than it.

An implicit global variable is what happens when you use a variable without first declaring it. In compiled languages this would result in a compilation error, but javascript silently declares the variable as a property of the global object (in a browser this is the window object)

$('#timeIn').on('change', function() {
    var numIn; // only available from inside this anonymous handler function
    ... snip ...
    timeIn = $('#timeIn').val(); // this is an implicit global since it has not been declared anywhere
    // an explicit global, for example's sake
    window.someVar = 'foo';
});

With javascript v1.7 you can also establish block scopes via the let keyword:

let(a = 5, b = 1) {
    // a and b are scoped to this block
    console.log(a+b); // 6
}
console.log(a+b); // error
jbabey
  • 45,965
  • 12
  • 71
  • 94
2

There are only 3 scopes in JavaScript.

x = 1; <- x is in global scope

var, when used outside of a function, will also create a global:

<script type="text/javascript">var x = 1;</script> <- x is in global scope

function () { var x = 1; } <- x is in function scope

for (let i = 0; i < 5; i += 1) {} <- x is in block scope

Lucas Green
  • 3,951
  • 22
  • 27
  • A function body is a block, so it is still block scope. – lanzz Sep 06 '12 at 17:17
  • @pst, that's the whole context. – Lucas Green Sep 06 '12 at 17:18
  • 2
    @lanzz No. Because C has "block scope"; e.g. `if (y) {int x; x = 1;}` –  Sep 06 '12 at 17:18
  • 1
    @Mythril My point is that there is not enough context given in this reply :) It is not the assignment that makes it global, it is the *lack* of being scoped otherwise. Including more context in this reply will remove the ambiguity. –  Sep 06 '12 at 17:18