Demonstrating Javascript “Strict” Mode

As we already know that javascript is a loosly typed language which means that you don’t need for example to define a variable before using it but recently a new EcmaScript 5 feature launched which can be used to do more syntax checking which is “Strict” mode.

 “Strict” mode can easily statically-analyzable subset of EcmaScript which would be a good target for future versions of the language. Strict mode was also designed in the hope that developers who limit themselves to strict mode would make fewer mistakes and that the bugs they do make would manifest in more obvious ways.

 

Invoking strict mode

Strict mode applies to entire scripts or to individual functions. It doesn’t apply to block statements enclosed in {} braces; attempting to apply it to such contexts does nothing.

 

Strict mode for scripts

To invoke strict mode for an entire script, put the exact statement "use strict"; (or 'use strict';) before any other statements.

 

Strict mode for functions

Likewise, to invoke strict mode for a function, put the exact statement "use strict"; (or 'use strict';) in the function’s body before any other statements.

 

So How Javascript Behaves In Strict Mode

In “Strict” mode some rules must be followed otherwise the script will fail and these include:

Use of undeclared variable

In strict mode we cannot use an undeclared variable.

delete operator

Delete operator is used to delete user defined object properties and array elements. If we try to delete anything other than user defined object properties or array elements we get an exception.

Multiple same declaration of property

In strict mode you cannot declare same object properties more than once. Otherwise it will throw exception.

Duplicate parameter names

In strict mode you cannot declare same parameter name more than once.

Octal numeric literals and escape characters are not allowed

Octal numeric literals and escape characters are not allowed in strict mode.

read-only and get-only Property

Writing to read-only and get-only properties of an object throws exception.

“eval” and “arguments”

We cannot use eval and arguments strings as variable, property or object names in strict mode.

“with” statement

Use of “with” is not allowed in strict mode.

Reserved keywords

In strict mode we cannot use some words which are reserved for future version of JavaScript.

Reserved keywords are:

  1. implements
  2. interface
  3. package
  4. private
  5. protected
  6. public
  7. static
  8. yield

“this” keyword

“this” keyword inside global functions is undefined in strict mode.

eval creating variables

eval function creates variables in the scope from which it was called. But in strict mode it doesn’t.

 

Benefits of strict mode

  • Strict mode changes previously accepted “bad syntax” into real errors.
  • In JavaScript mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a property of window variable.
  • In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties. And also any assignment to a non-writable property, a getter-only property, a non-existing property, a non-existing variable, or a non-existing object, will throw an error.

Share this: