Skip to Content
Author's profile photo Klaus Haeuptle

Getting more out of your JavaScript unit tests with strict mode

JavaScript has many pitfalls. In strict mode you can detect some bugs earlier. Strict mode makes several changes to normal JavaScript semantics:

  • First, strict mode eliminates some JavaScript silent errors by changing them to throw errors. E.g. makes it impossible to accidentally create global variables. In normal JavaScript mistyping a variable in an assignment creates a new property on the global object and continues to “work”.
  • Second, strict mode prohibits the use of keywords which are likely to be defined in future versions of ECMAScript, such as ‘implements’, ‘private’, and ‘protected’.

When you start a new application, you can switch on strict mode from the beginning.

For more information about strict mode see:

You should, however, be careful when using strict mode in applications with an existing codebase. Since some of the errors are only detected during runtime, switching to strict mode for your existing productive code without testing everything can be risky. If you cannot ensure all code is touched by automated tests, you might not want to enable strict mode for your productive code. Instead you can use the strict mode for executing your unit tests to detect some bugs earlier without putting strict mode in the productive implementation.

How to? You can use a self-executing anonymous function to put your test in strict mode. Douglas Crockford describes the reasons for using the self-executing function in his blog post:

( function() {

  “use strict”;

  module( “Assign List”, {

   setup : function() {







This blog post is part of a series, like the following blog post to stay tuned and get updates about more topics around software engineering with SAPUI5 and JavaScript:

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Prerana Chincholi
      Prerana Chincholi

      Hello Klaus,

      Thanks a lot for this blog post. I appreciate your dedication.