3

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.

Ryan
  • 782
  • 1
  • 10
  • 25
  • 3
    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 doing so. Always best to declare explicitly, even if global. – Nick Oct 14 '17 at 21:17
  • 3
    Possible duplicate of [What is the purpose of the var keyword and when to use it (or omit it)?](https://stackoverflow.com/questions/1470488/what-is-the-purpose-of-the-var-keyword-and-when-to-use-it-or-omit-it) – Gerardo Oct 14 '17 at 21:18
  • I took a look at the link from Gerado, and read what snapjs wrote, I think that pretty much clears it up. Thanks! – Ryan Oct 14 '17 at 21:21
  • Possible duplicate of [What is the purpose of the var keyword and when to use it (or omit it)?](https://stackoverflow.com/questions/1470488/what-is-the-purpose-of-the-var-keyword-and-when-to-use-it-or-omit-it) – Unamata Sanatarai Oct 14 '17 at 21:23
  • Also see [*var functionName = function() {} vs function functionName() {}*](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname/336868#336868). – RobG Oct 14 '17 at 21:24

3 Answers3

2

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!
Nick
  • 16,066
  • 3
  • 16
  • 32
  • 1
    That isn't a function declaration, it's a function expression. And you should declare *a*. ;-) – RobG Oct 14 '17 at 21:23
2

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

vsync
  • 118,978
  • 58
  • 307
  • 400
2

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.

linasmnew
  • 3,907
  • 2
  • 20
  • 33
  • Thanks everyone for answering. I accepted linas answer as it makes it nice an clear this hoisting stuff. Never really understood that. This is a nice answer. – Ryan Oct 14 '17 at 21:33