JavaScript, as one of the main languages on the web, regularly receives updates which improve its functionality. As a rule, they introduce compatibility with new hardware or optimised methods for writing and compiling code. Standards authors try to maintain code compatibility, but sometimes adding new functionality requires abandoning existing features. Also over time new restrictions and rules may appear in code writing, which may have an impact on standards too.
For example, in ECMAScript 5, the “strict mode
” option was added. Adding this option to a script overrides or changes a number of rules for interpreting JavaScript code. It was added as an optional feature to save the ability to interpret scripts written without using it. Since strict mode disables the ability to use some coding practices, in some cases it is used to create specially optimised and protected scripts. Scripts in strict mode are compatible with all modern browsers, except Internet Explorer before 10 versions.
How does the strict mode work in JavaScript?
To enable strict mode, just add the following line to your code:
'use strict ';
Strict mode can be applied to the entire script or just part of it. In the first case you must specify this line at the beginning of the script before declaring all variables. To use strict mode for a function, you must enable it on the first line within the function's code block.
There is no limit to the number of functions within a script for which strict mode can be enabled. This is often used to create scripts which contain logic for both work modes.
Also, strict mode significantly affects JavaScript code semantics, and in some cases syntax.
It is worth noting that it doesn’t have special commands or methods for deactivating, so you can disable strict mode only by removing the activation string. It is also not recommended to add a strict mode activation string "in the middle" of the script/function code, as it can cause errors and incorrect code interpretation.
JavaScript rules in strict mode
1. You must always declare variables and objects
Strict mode forbids declaring a variable (or object) after assigning a value, or, as in older versions of JavaScript, not declaring it at all. It is worth noting that this is one of the rules that programmers keep without using strict mode too.
Will throw an error (in strict mode):
num = 1;
word = ‘abc’;
Can be used:
let num;
num = 1;
const word = ‘abc’;
2. Strict mode adds extra reserved words
The following words cannot be used as names of variables and functions: implements
, interface
, let
, package
, private
, protected
, public
, static
, yield
. For example, if we declare a variable named "interface
" then it will throw an error, but we can use this word if we add at least one character difference. For a complete list of all reserved words in JavaScript, follow this link.
Will throw an error (in strict mode):
let interface = 1;
Can be used:
let Interface;
let interface2;
let _interface;
3. You can’t use numbers in the octal system
In most browsers, octal numbers are created by adding a 0 in front of them. But some programmers put 0 in front of the number, meaning that it would remain in the decimal system, which led to errors in the calculations. So restrict mode disables this possibility on the whole.
For example this
let test = 0021;
is not the same that
let test = 21;
Note: You can still declare numbers in the octal system using “o
”, for example 0o21
.
4. You can't use the ‘with’ statement
This is due to the fact that in some cases it’s impossible to determine what with
will refer to. In strict mode, with
statements will be treated as an error.
With statement syntax example:
with object(){
let a = b;
}
5. You can't use the ‘eval’ statement to create new variables
In strict mode, creating variables via eval
is not allowed. However, this word remains reserved and cannot be used for variable and function names either.
eval
statement syntax example:
eval ("let a = 10 ");
6. Some cases of using the delete statement are unavailable
In strict mode, using the delete
statement to remove simple names, objects and their properties, would be considered a syntax error.
Will throw an error (in strict mode):
let a = 1;
let b = {country: France, capital: Paris};
delete a;
delete b;
7. You can't duplicate function parameters and object properties
For example, we created a date
function, and named its parameters: day
, day
and year
. Since the names of the day
parameters are the same, it can be confusing which one is meant at a particular point in the function.
Therefore, in normal mode, it will always default to the value of the second day
, since it was declared later. While in the strict mode creation parameters with the same name is forbidden.
Will throw an error (in strict mode):
function date(day, day, year){
return day;
}
The same rules apply and to object properties, too:
let date = { month: 1, month: 2 };
8. You can't set properties of primitive values
In JavaScript, primitives are: BigInt, boolean, number, string, symbol, null, undefined.
Will throw an error (in strict mode):
(151).box = 'red';
'text'.words = 'next sentence';
9. No arguments.callee available
arguments.callee
is used when you need to refer to a function within the same function, at the moment when it is executed.
Will throw an error (in strict mode):
function Test () {
let a =5;
return arguments.callee;
};
10. Failed assignments will throw an Exception message
In JavaScript, some variables cannot be assigned because they are write-protected, read-only, or otherwise. In normal mode, code with such actions will be ignored by the interpreter. In strict mode, an Exception message will be returned for each case.
let undefined = 'defined';
Test yourself
Test 1
Fix broken code to make it correct to use within strict mode.
basket = {fruits: 004, vegetables:005};
let eval;
Sum();
function Sum(){
eval = basket.fruits + basket.vegetables;
console.log(eval);
}
Solution
We activate strict mode
at the beginning of the code. Then we fix the declaration of the basket
variable by adding “let
” in front of it. We also remove the zeros before “fruits
” and “vegetables
”, as otherwise these will be numbers in the octal system, which is prohibited in strict mode
. And the last step is to change the name of the eval
variable in all places where it is mentioned to sum, because it is a reserved word.
"use strict";
let basket = {fruits: 4, vegetables:5};
let sum;
Sum();
function Sum(){
sum = basket.fruits + basket.vegetables;
console.log(sum);
}
Result
9
Task 2
Fix broken code to make it correct to use within strict mode.
let package = 100;
package = 500;
Sum(package, package);
function Sum(pack, pack){
sum = pack + pack;
console.log(sum);
}
Solution
First, let's add a line at the beginning of the code to activate the strict mode ('use strict
'). The first mistake is to use the reserved word “package
” for variable names. To fix this, we will add “_” characters before their names. Then we will add another character (”1” and “2”) at the end to fix the error with the same variable names. Also in strict mode, you need to declare a variable, so before ”_package2
“ will be “let
“.
Now after fixing the variables at the start, we will fix them in other places in the code, in particular in the fourth line. In the Sum
function, we will also change the names of the parameters, because they have the same names, which is prohibited in strict mode. And the last error – not declared type the sum
variable, so add “let
” in front of it.
After executing the code, we will receive the result of summing up our variables (600) in the console.
'use strict';
let _package1 = 100;
let _package2 = 500;
Sum(_package1, _package2);
function Sum(pack1, pack2){
let sum = pack1 + pack2;
console.log(sum)
}
Result
600