Some typical actions that are forbidden in strict mode are given below:
1. Undeclared objects or variables.
We have already seen that using a variable or an object without declaring it is not allowed in strict mode.
Example:
"use strict";
user = {
name: 'John',
age: 30,
gender: 'male',
}
console.log(user.name) // John
Output:
user = {
referenceError: User is not defined
2. Deleting an object, variable, or function.
Example:
"use strict";
let user = {
name: 'John',
age: 30,
gender: 'male',
}
delete user; // Error here
Output:
delete user = {
SyntaxError: Delete of …..
3. Duplicating a parameter name.
Example:
"use strict"; functionhello(value, value){ console.log(value + " "+ value);} |
Output:
function hello(value, name){
user.name = “John”;
4. Writing to a read-only property, get-only property & undeletable property is not allowed.
Example:
"use strict";
const user = {};
Object.defineProperty(user, "name", {value:0, writable:false});
user.name = "John";
Output:
user.name = “John”;
TypeError: Cannot …
4. Octal numeric literals & Octal escape characters are not allowed.
Example:
"use strict";
letnum = "\010";
// Error here
Output:
let
num = "\010"; // Error here
SyntaxError:…
5. ‘eval’ cannot be used as a variable.
Example:
"use strict";
let eval = "Hello World";
console.log(eval);
Output:
let
eval = "Hello World";
SyntaxError:…
6. ‘arguments’ cannot be used as a variable.
Example:
"use strict";
let arguments = "Hello World";
console.log(arguments);
Output:
let arguments = “Hello World”;
SyntaxError:…
7. ‘with’ is not allowed.
Example:
"use strict";
with ([1, 2, 3]) {
console.log(toString());
}
Output:
with ([1, 2, 3]) {
SyntaxError:…
8. Keywords such as implements, interface, static, package, private, protected, public, let and yield are prohibited in strict mode.
Example:
"use strict";
let public = "public";
Output:
let public = “public”;
SyntaxError:…
46