Javascript – A quick introduction into strict mode

What is strict mode?

Strict mode is a new feature added in EcmaScript2015 (the latest version of Javascript). The main purpose is to help the developers write ‘more secure’ code. When running in strict mode, operations which are considered in the ‘to be avoided’ category, will cause an error in strict mode.

How to run code in strict mode?

You can run the code from an entire script or a function by adding the syntax "use strict;"  or 'use strict;' at the start of the script or function.

This expression was specially chosen so it won’t cause an error on older browsers. The great think about strict mode is that you won’t have to worry about browser compatibility – the modern browsers will use it to stop you making errors and the older ones will simply ignore it -.

Not allowed in strict mode

  • using a variable without declaring it
  • delete a variable (or object)
  • delete a function
  • duplicate a parameter  – func(x1, x1)
  • octal numeric literals var x = 010;
  • escape character x = \010;
  • writing to a read-only property
  • writing to a getter property
  • deleting an undeletable property
  • declaring variables via eval() – eval(“var x;”);
  • use some strings as variables (names that have meaning in JS or future keywords) – eval, arguments, implements, interface, let, package, private, public, protected, static, yield

Since most of these situations are a real pain to be debugged when they pass the compilation and strict mode is backwards compatible, I believe it’s a good idea to start using right away.

Note: This is part of Javascript learning roadmap.

 Please subscribe for more weekly web wisdom!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s