What is 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 –
- 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() –
- 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.
Please subscribe for more weekly web wisdom!