Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are functions defined regardless of the order?

Tags:

javascript

I'm not really a javascript noob at all, although in my whole life I've never came across this, but am I right in assuming that javascript must assign functions before running anything or something?

In all my experience, I expected this to return 'undefined', but obviously it returns 'function'.

function bar() {
    return foo;
    foo = 10;
    function foo() {}
    var foo = '11';
}
alert(typeof bar());

Is someone able to explain this for me?

like image 794
Shannon Hochkins Avatar asked Jul 10 '15 07:07

Shannon Hochkins


People also ask

Does order matter in a function?

The order of functions starts to matter hugely when functions are declared and invoked inside other functions (these are also called closures).

How do you know if a function is defined or not?

Use the typeof operator to check if a function is defined, e.g. typeof myFunction === 'function' . The typeof operator returns a string that indicates the type of a value. If the function is not defined, the typeof operator returns "undefined" and doesn't throw an error.

Where are functions not defined?

A function is said to be "undefined" at points outside of its domain – for example, the real-valued function. is undefined for negative. (i.e., it assigns no value to negative arguments). In algebra, some arithmetic operations may not assign a meaning to certain values of its operands (e.g., division by zero).

How are functions defined in JavaScript?

A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)


1 Answers

This behaviour of JavaScript is called hoisting. There is a good explanation on the MDN (https://developer.mozilla.org/en-US/docs/Glossary/Hoisting)

In JavaScript, functions and variables are hoisted. Hoisting is JavaScript's behavior of moving declarations to the top of a scope (the global scope or the current function scope).

That means that you are able to use a function or a variable before it has been declared, or in other words: a function or variable can be declared after it has been used already.

Basically, if you declare a variable like this:

console.log(s);             // s === undefined
var s = 'some string';

s declaration will be "hoisted" to the beginning of the scope (i.e. there will be no ReferenceError on the line with console.log). The variable's value will not be defined at that moment though.

The same goes with assigning an anonymous function to a variable, so:

console.log(f);             // f === undefined
f();                        // TypeError: f is not a function
var f = function () {};     // assigning an anonymous function as a value
f();                        // ok, now it is a function ;)

The variable will be hoisted and thus visible in the entire scope, but it's value, even if it's a function, will still be undefined - hence the error if you try to execute it.

On the other hand if you declare a named function:

console.log(f);             // f === function f()
f();                        // we can already run it
function f() {};            // named function declaration

It's definition will also be hoisted so you can run it even in the first line of the scope you've declared it.

like image 124
Tomek Sułkowski Avatar answered Sep 25 '22 22:09

Tomek Sułkowski