Introduction
There are several other new features and APIs that need attention, as well, the largest of which are Strict Mode and native JSON support.
Strict Mode
Strict Mode is a new feature in ECMAScript 5 that allows you to place a program, or a function, in a “strict” operating context. This strict context prevents certain actions from being taken and throws more exceptions (generally providing the user with more information and a tapered-down coding experience).
Since ECMAScript 5 is backwards-compatible with ECMAScript 3, all the “features” that were in ECMAScript 3 that were “deprecated” are just disabled (or throw errors) in strict mode, instead.
Strict mode helps out in a couple ways:
- It catches some common coding bloopers, throwing exceptions.
- It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).
- It disables features that are confusing or poorly thought out.
Most of the information about Strict mode can be found in the ES5 specification [PDF] on page #235.
It should be noted that ECMAScript 5’s Strict mode is different from the Strict mode available in Firefox (which can be turned on by going to about: config and enabled javascript.options.strict). ES5’s strict mode complains about a completely distinct set of potential errors (whereas Firefox’s existing strict mode tries to enforce some good practices, only).
How do you enable strict mode?
Simple. Toss this at the top of a program to enable it for the whole script:
"use strict";
Or place it within a function to turn on strict mode only within that context.
- function imStrict(){
- "use strict";
- // ... your code ...
- }
Note the syntax that’s used to enable strict mode (I love this!). It’s simply a string in a single statement that happens to contain the contents “use strict”. No new syntax is introduced to enable strict mode. This is huge. This means that you can turn strict mode on in your scripts – today – and it’ll have, at worst, no side effect in old browsers.
As you may note from the examples here and ECMAScript4 there are virtually no new syntax additions or changes to the language in ECMAScript 5. This means that you can write your ES5 scripts in a manner that will be able to gracefully degrade for older useragents – something that wasn’t possible with ECMAScript 4. The way in which strict mode is enabled is a great illustration of that point in practice.
A neat aspect of being able to define strict mode within a function is that you can now define complete JavaScript libraries in a strict manner without affecting outside code.
- // Non-strict code...
- (function(){
- "use strict";
- // Define your library strictly...
- })();
- // Non-strict code...
A number of libraries already use the above technique (wrapping the whole library with an anonymous self-executing function) and they will be able to take advantage of strict mode very easily.
So what changes when you put a script into strict mode? Several things.
Variables and Properties
An attempt to assign foo = “bar”; where ‘foo’ hasn’t been defined will fail. Previously it would assign the value to the foo property of the global object (e.g. window.foo), now it just throws an exception. This is going to catch some annoying bugs.
Any attempts to write to a property whose writable attribute is set to false, delete a property whose configurable attribute is set to false, or add a property to an object whose extensible attribute is set to false will result in an error. Traditionally no error will be thrown when any of these actions are attempted, it will just fail silently.
Deleting a variable, a function, or an argument will result in an error.
- var foo = "test";
- function test(){}
- delete foo; // Error
- delete test; // Error
- function test2(arg) {
- delete arg; // Error
- }
Defining a property more than once in an object literal will cause an exception to be thrown
- // Error
- { foo: true, foo: false }
eval
Virtually any attempt to use the name ‘eval’ is prohibited – as is the ability to assign the eval function to a variable or a property of an object.
- // All generate errors...
- obj.eval = ...
- obj.foo = eval;
- var eval = ...;
- for ( var eval in ... ) {}
- function eval(){}
- function test(eval){}
- function(eval){}
- new Function("eval")
Additionally, attempts to introduce new variables through an eval will be blocked.
- eval("var a = false;");
- print( typeof a ); // #ff0000
Functions
Attempting to overwrite the arguments object will result in an error:
- arguments = [...]; // not allowed
Defining identically-named arguments will result in an error function( foo, foo ) {}.
Access to arguments.caller and arguments.callee now throw an exception. Thus any anonymous functions that you want to reference will need to be named, like so,
- setTimeout(function later(){
- // do stuff...
- setTimeout( later, 1000 );
- }, 1000 );
The arguments and caller properties of other functions no longer exist – and the ability to define them is prohibited.
- function test(){
- function inner(){
- // Don't exist, either
- test.arguments = ...; // Error
- inner.caller = ...; // Error
- }
- }
Finally, a long-standing (and very annoying) bug has been resolved: Cases where null or undefined is coerced into becoming the global object. Strict mode now prevents this from happening and throws an exception instead.

Join the conversation! Your thoughts help the community grow.