Javascript - What is "use strict"? What does it do? and why it is a good idea to use it

Simply put, 'use strict' forces you to write more clean code.

You might have come across this in javascript 'use strict' or "use strict". You might be wondering what it does and what function it has. 'use strict' defines that your javascript code should be run in 'strict mode'. Which means that your code has to comply with more strict standards. If it does not it will cause a syntaxError.

How can I use it?

There are two ways 'use strict' can be initialized. It can be used at top of your javascript or at the beginning of a function:

  • Beginning of a function: Setting 'use strict' in the beginning of a function limits it to that function. This has the advantage of not interfering with any other code you might have. Sometimes you might have several libraries included. Some may contain code that do not comply with the 'use strict' standards. When wrapping your code in a function with strict mode, all other code will not be affected by strict mode. This is also ideal if your code is wrapped in function, this makes all your code run in strict mode, but libraries you use are not affected.
  • Beginning of javascript: Setting 'use strict' at the beginning of your javascript will make it global. Which means all javascript code executed will be in strict mode. This has the disadvantage of forcing all other code (libraries) you use to comply with strict mode. Which they might not. This can cause syntax errors. However setting it globally has the advantage that it only has to be set once.

A great way to use strict mode without interfering with any existing code is to create an "immediate function". This is also a great way to not tamper with the global scope:

// Not in strict mode
(function(){
  "use strict";
  // strict mode - Set up your new code here
})();
// Not in strict mode

What does it do?

The use of 'use strict' has a series of effects on your code. The following items can cause an error in strict mode - and you will not be able to run the code if it has any of these errors:

  • Creating variables without using var: Creating variables like the following x = 1 is not allowed. This is to ensure that new global variables are not created accidentally.
  • Having 2 functions parameters with the same name: When in strict mode having a function with 2 parameters with the same name throw an error. This could be function(s, s){}
  • Having the same 2 property names on an object literal Like functions' parameters, object literal properties must unique under strict mode. As in you cannot create an object like var k = { b:'a', b:'c' };
  • Assigning to read only variables: In strict mode read only assignments throw an exception. Such as assigning a value to NaN - as in NaN = 10;. This would otherwise fail silently.
  • Deleting on variables which cannot be deleted: using delete on properties such as prototype fails with an exception under strict mode. Where it would fail silently normally.
  • Using arguments.callee: argument.callee is not supported.
  • Using with: The with keyword is forbidden when in strict mode.
  • Octal syntax: Octal syntax is forbidden in strict mode.

You can find the full list here.

Browser support

'use strict' is widely supported and is supported in all modern browsers. You can see this on caniuse.com. Browsers without 'use strict' support will just ignore it. It will not cause any errors.

I hope you found this useful, let me know in the comments down below!