Understand Scopes and Strict Mode in JavaScript…30daysofjavascript => Day12

A scope is what determines whether or not variables stored can be accessed, in other words it determines the accessibility of a variable. There are two known type of scope namely local scope and global scope.

  • Local Scope: This scope is the one that can only be accessed within a block of enclosed code. For instance, a variable declared in a function or an if else statement can only be accessed in the block of code and any attempt to access the variable outside the block of code will return undefined.
  • Global Scope: This scope can rather be accessed anywhere, it can be accessed at the last line of written codes even if it was declared at the first line of code.

Now lets examine some examples

Local scope/variable

In the above example, two different variables were declared in two different functions and are accessed inside and outside the functions.

Global scope/variable

In global scope we can always retrieve data that are stored in a variable anywhere. Also values assigned to undeclared variables automatically gives a global variable

automatic global variable

In strict mode undeclared variables does not automatically give a global variable, how? Now this takes us to strict mode.

Strict Mode

Normally, JavaScript do not return errors when a value is been assigned to a non-writable property, getter only property, non existing property, non existing object or even non existing property but in strict mode, any assignment to the above conditions will throw an error. Strict mode allows us to write more secure JavaScript as well as make several changes to normal JavaScript semantics:

  • Eliminates some JavaScript silent errors by changing them to throw errors.
  • Fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that’s not strict mode.
  • Prohibits some syntax likely to be defined in future versions of ECMAScript.

What is not allowed in JavaScript strict mode?

  • Using an object, without declaring it, is not allowed
  • Deleting a variable (or object) is not allowed
  • Deleting a function is not allowed
  • Duplicating a parameter name is not allowed
  • Octal numeric literals are not allowed
  • Octal escape characters are not allowed
  • Writing to a read-only property is not allowed
  • Writing to a get-only property is not allowed
  • Deleting an undeletable property is not allowed
  • The word eval cannot be used as a variable
  • The word arguments cannot be used as a variable
  • The with statement is not allowed

You can try the above code snippet in your IDE, run in browser and and use the debug console (click F12 ) to check errors. These are just few examples make sure you try more examples on your own and see the errors they return. Example of errors from codes like this is seen below:

errors from strict mode

Reserved Keyword

There are reserved keywords for future JavaScript version that can not be used as variable name. The keywords are listed below:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield

What is this about?

30daysofjavascript is a series of writing on how I learn to code in JavaScript. These episodes are as simplified as possible and for beginners like me, I hope you find JavaScript less confusing throughout this episode. Thank you as always see you in the next episode. Check out every episode I have written here.

Front end web developer