How to use strict in JavaScript
-
use strict
in JavaScript -
Implement
use strict
in JavaScript -
Advantages of Implementing
use strict
in JavaScript
This tutorial will discuss the use strict
feature in JavaScript. Here we will see how to create and execute the use strict
keyword in our JavaScript code statements with different examples.
use strict
in JavaScript
In the JavaScript version, ECMAScript 5, strict mode is a new feature introduced that will help us execute a set of programs and their functions in a strict operating context. We use strict context to restrict various actions from being executed and throw more exceptions.
We give instructions to the browser to use strict mode by the statement use strict
. It is a minimal and safer feature set of JavaScript.
Implement use strict
in JavaScript
Remember that we can’t use strict mode with block statements enclosed in braces. There are two ways mentioned below to use strict mode:
- We can use it in the global scope for the entire script.
- We can use it in individual functions.
Implement use strict
for the Entire Script in JavaScript
Before any other statements, we put the exact statement use strict
to invoke strict mode for the whole script.
Syntax:
// strict mode syntax for Whole-script
'use strict';
let a = 'script for strict mode!';
Implement use strict
for Individual Functions in JavaScript
In the function body, before any other statements, we put the exact statement use strict
to invoke strict mode for a function.
Syntax:
function strictFunction() {
// strict mode syntax for Function
'use strict';
function nestedFunction() {
return 'Javascript on DelftStack';
}
return 'strict mode functions! ' + nestedFunction();
}
function notStrictFunction() {
return 'non strict function';
}
Strict Mode Example
As we know, mistyping a variable name creates a new global variable in normal JavaScript. In strict mode, it will throw an error.
Example:
<!DOCTYPE html>
<html>
<body>
<p>function will cause errors while using `use strict` in that function.</p>
<p>To see the error reprt just activate debugging in your browser by pressing (F12).</p>
<script>
a = 10; // This will not cause an error.
myFunction();
function myFunction() {
"use strict";
b = 10; // This will cause an error (b is not defined).
}
</script>
</body>
</html>
We have defined <script>
tags in this HTML page source to use JavaScript code statements. Inside the <script>
tag we simply initialized undefined variable a
with numeric value 10 and called myFunction()
.
After that, we created a declaration of myFunction()
using the use strict
keyword. We also initialized the undefined variable b
with a numeric value of 10 to test the execution with the use strict
mode.
You can save the given HTML example with the .html extension and open it in any browser to check the output. You need to activate debugging mode of your browser by simply pressing F12 to see the error report.
Output:
the function will cause errors while using `use strict`.
To see the error report, activate debugging in your browser by pressing (F12).
Advantages of Implementing use strict
in JavaScript
In simple JavaScript semantics, the strict mode makes various changes. We can eliminate a few silent errors of JavaScript by changing them to throw an error using strict mode.
- To perform optimizations, we can fix mistakes that make it difficult for JavaScript engines.
- Code of strict mode can sometimes execute faster than normal code, which is not with strict mode.
- In the future version of ECMAScript, strict mode prevents some syntax likely to be defined.
- Strict mode prevents any unsafe actions, such as trying to gain access to global variables or objects.
- Strict mode disables confusing features or such features that are poorly thought out.
- To write secure JavaScript more efficiently, we use strict mode.