Previously, Javascript kept evolving without changing the old functionality. However, in 2009, ECMAScript 5 appeared and that fixed functionality. By default, the EXMAScript 5 is disabled, but if you use the use strict
keyword, the modifications will be placed.
Strict mode makes several changes to JavaScript semantics including:
The syntax is:
"use script"
//modern implementation of the code
Remember:
use script
should be at the top of the scripts to enable strict mode.
One key thing to note is that once in strict mode, you can not go back to old mode as no such command exists.
JavaScript classes and modules automatically make use of use script
so that there is no need to shift to script mode.
If you assign values to an undeclared variable, it gives an error:
// adding value to an undeclared variable xx = 300; //no errorprint();function print(){"use strict";y = 300; //undeclared- gives errorconsole.log(y)}
Similarly, deleting a variable or a function also gives an error:
var x = 300;print();function print(){"use strict";y = 300; //undeclared- gives errorconsole.log(y)}//delete x //gives errordelete print //gives error
JS does not allow octal numeric literals, octal escape characters, or writing to a read-only property.