Do I have to use `var` when declaring a function in javascript?

Question

Consider the following 2 functions:

cat = function() {
  console.log("Meow");
} 

and:

var dog = function() {
   console.log("woof")
}

cat() -> "Meow"

dog() -> "Woof

They both work, except that cat is not declared using var. Does this mean that it is globally scoped? I would say that both functions are globally scoped. There is also the syntaxfunction cat(){...}, I guess that is similar to the first style, some sort of implicit variable binding...

Could somebody explain the difference between the styles for declaring functions, if there is any.


Show source
| javascript   2017-10-14 23:10 3 Answers

Answers to Do I have to use `var` when declaring a function in javascript? ( 3 )

  1. 2017-10-14 23:10

    Not using var/let/const makes it implicitly global, which is generally regarded as a bad thing. If you use 'use strict', you'll get an error for any implicit globals. The biggest issue that arises with implicit global variables is that you may not know that you've made a global variable. For example:

    (function() {
       a = 5;
    })();
    
    // a doesn't exist right?
    console.log(a); // 5... whoops!
    
  2. 2017-10-14 23:10

    No you don't. you can declare a function like so:

    function foo(){
    }
    

    Then foo is automatically declared at the appropriate scope. Its all a matter of scopes. where will the interpreter declare the function. Doing it the way you did it, without var will cause the interpreter to create a global variable automatically, and that is the highest scope possible, meaning that it is accessible everywhere in the code. That is considered a bad thing, since you normally wouldn't want to do that unless it is done intentionally, because of deep reasons which I can go into if you wish.

    function foo(){
      function bar(){
        console.log("bar");
      }
      
      bar();
      
      console.log(typeof bar);
    }
    
    foo();
    bar(); // will throw an error since "bar" does not exist at this scope


    Read all about function declaration

  3. 2017-10-14 23:10

    Variables

    • If you don't specify var, let or const it will get globally scoped
    • If you do specify var, let or const then it will get scoped to the nearest enclosing scope depending on that particular specifier

    (var - will get scoped to the nearest enclosing function or global scope if not defined inside of a function)

    (let & const - will get scoped to the nearest enclosing block)

    Functions

    Assigning a function as follows:

    var dog = function() {
       console.log("woof")
    }
    

    Means that the function will not be accessible until the line that it is declared on is reached during execution, i.e. you will only be able to execute this function from after the line on which it was declared.

    Whereas declaring a function as follows:

    function cat(){...}
    

    Means that it will be moved to the top of the enclosing scope, so you will be able to call it from anywhere within the reachable scope even if it's earlier in code than the line on which you declared it on.

Leave a reply to - Do I have to use `var` when declaring a function in javascript?

◀ Go back