Var 1Correct__ syntax = Function{ return question,} ( askquestion):

So this has been bugging me for quite a while now.

(javaScript)

In codecademy we are taught that this is the correct syntax of a function:

var hello_World = function(para1, para2){

//code here
};

or

function hello_World(para1, para2){

//code here
};

But the way I was taught (at school)
was:

var hello_World = function(para1, para2){

//code here
}

or

function hello_World(para1, para2){

//code here
}

Notice the difference?

No semicolon after a function.

This will lead you to the official Mozzila website, and even there no semicolon is used.

http://www.w3schools.com/js/js_functions.asp

This will lead you to w3schools, which is also another great site to learn javascript, and even there no semicolon is used.

So why does codecademy teach us different? What is the purpose of keeping that semicolon
(if any at all)? Is codecademy potentially teaching the wrong code?

function declaration

function foo(bar) {

}

foo and the function it identifies are immutable, I believe. Even if not, we don’t expect to want or need to change it. Plus it is hoisted.

function expression statement

var foo = function (bar) {

};

Statements end with a semi-colon. The function is an anonymous expression. It only exists while the foo referrence to it exists. foo could be changed at any time. Its function is not hoisted.

but what effect would

function foo(bar) {

};

againt

function foo(bar) {

}

(no semi colon have)

and also I am a novice programmer, so I have no idea whta hoisted means, or anonymus expression, or immutable. Could you please clairify?

We don’t add a semi-colon to the declarative form. It doesn’t need it, even in minified form. An expression does, though, else it cannot be minified.

minified code has all white space removed. It will throw errors if any semi-colons are missing. Remember line breaks are white space, too.

Hoisting relates to scope, so until that topic comes up, it will be difficult to explain.

An anonymous expression is one without an identifier. It is held in memory only as long as a variable references it. Change the variable reference and the expression vanishes. This too will come up again in your studies and will be easier to grasp by that point.

immutable means it cannot be changed. We cannot change a function once it is hoisted to the top of its scope. When you get into Objects you will learn that keys are immutable. Strings are immutable. We cannot remove or rearrange the letters in a string. For that we use splicing and substrings to remove or rearrange, or an array for sorting.

Objects and arrays are mutable, which means they can be changed on the fly.

function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
        console.log("I am a " + this.adjective + " rabbit");
    };
}

I found this code while I was doing the functions tasks here on codecademy, so from this I can tell ( like what you said) that when there is an equal sign there is a semi colon?

Also I know about global and local variable scopes, does that have anything to do with hoisting?

And also thank you for the great answer, really helps out, but im a novice so I got more questions for you! :wink:

1 Like

Correct. It is not a function statement, but a function expression statement which means it is an assignment. Assignments always get a semi-colon.

Up until 2015, functions were the only objects with block scope. The JS we are working with here is the older version, ES5 (ECMAScript 5), so this is the behavior we may still expect. In newer JS versions, all blocks have their own scope, meaning if, for, while, switch and any other construct that has a block are now added to functions. But we won’t concern ourselves with that, for now.

Scope is another way of saying, environment variables. Variables cannot be accessed outside of the environment where they are declared. All declared variables and objects are hoisted (moved to the top) of their relative scope. Values and expressions are not hoisted, but function declarations are. This is akin to moving everything to the top of the code heap within a given scope.

Because of hoisting, we can reference a function or variable before it is defined in the code.

foo();    // I'm hoisted
function foo() {
    console.log("I'm hoisted!");
}

bar();    // ReferenceError
var bar = function() {
    console.log("I am not hoisted.");
};

You’re welcome!

Again WOW, you are amazing! Thank you for sharing your knowledge! I have a better understanding of javascript thanks to you! :smiley:

1 Like