Scoping Limitations – Lexical Scope 3

Moving onto section 3

A confusing aspect of JavaScript is that variable assignment can occur which have NOT been declared before.

e.g.

var hero = aHero();

var newSaga = function()

{

foil = aFoil();

};

log(foil);

Note that foil has not been declared anywhere before.  Variables which are being assigned for the first time will automatically be global and not to the scope where it was assigned.  This is BAD PRACTISE and is frowned upon in programming.

Unlike other languages NOT all curly, {}, braces are relevant for scoping.  Only the curly braces on a function statement creates scope NOT for if statements or while statements.

Scoping limitations can be very useful, as it allows us to think less of our program all at once.  The best practise is to use the var keyword at declaration and not follow the example given above.

 

This section of the course was not straight forward at first but after going through it again, it was just saying that “JavaScript can let you get away with using a variable which has not been created, but please don’t do so as it is confusing and can cause headaches to those using the script after you.”

Leave a Reply

Your email address will not be published. Required fields are marked *