CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

JavaScript - Strict Mode

JavaScript - Strict Mode

Strict Mode in JavaScript

In JavaScript, thestrict modeis introduced in the ES5 (ECMAScript 2009). The purpose behind introducing the β€œstrict mode” is to make the JavaScript code more secure.

The β€˜use strict’ literal expression is used to add the strict mode in the JavaScript code. It removes the silent errors from the code, such as you can’t use the variable without declaration, you can’t modify the readable property of the object, etc.

Enabling Strict Mode

To enble strcit mode, you should write the following literal expression to the top of your code βˆ’

JAVASCRIPT CODE
'use strict';

The β€˜use strict’ directive is used enable JavaScript’s strict mode.

Why Use the Strict Mode?

Here, we have listed some reasons for using the strict JavaScript mode βˆ’

  • Error Preventionβˆ’ The strict mode prevents the common errors which developers make while writing the JavaScript code, such as initializing the variable without declaration or using the reserved keywords as an identifier.
  • Safer Codeβˆ’ The strict mode prevents the creation of global variables accidentally. Also, it doesn’t allow to use of statements like β€˜with’, which can lead to vulnerability in the code.
  • Future Compatibilityβˆ’ You can align your code with the future versions of JavaScript by using the script mode. For example, the current version of JavaScript doesn’t contain keywords like β€˜public’ but is reserved for future versions. So, the strict mode won’t allow you to use it as an identifier from now.

Strict Mode in the Global Scope

When you add the β€˜use strict’ at the top of the JavaScript code; it uses the strict mode for the whole code.

Example

In the example below, we have defined the β€˜y’ variable and initialized it with the 50. The code prints the value of β€˜y’ in the output.

Also, we initialized the variable β€˜x’ without declaring it. So, it gives theerrorin the console and doesn’t print the output.

In short, the strict mode doesn’t allow you to use the variable without its declaration.

Preview

Strict Mode in the Local Scope

You can also use the β€œstrict mode” inside the particular function. So, it will be applied only in the function scope. Let’s understand it with the help of an example.

Example

In the example below, we used the β€˜use strict’ literal only inside the test() function. So, it removes the unusual errors from the function only.

The code below allows you to initialize the variable without declaring it outside the function but not inside it.

Preview

Mistakes that you should’t make in the strict mode

  1. You can’t initialize the variable with a value without declaring it.
JAVASCRIPT CODE
<script>
   'use strict';
   num = 70.90; // This is invalid
</script>
  1. Similarly, you can’t use the object without declaring it.
JAVASCRIPT CODE
<script>
   'use strict';
   numObj = {a: 89, b: 10.23}; // This is invalid
</script>
  1. You can’t delete objects using the delete keyword.
JAVASCRIPT CODE
<script>
   'use strict';
   let women = { name: "Aasha", age: 29 };
   delete women; // This is invalid
</script>
  1. You can’t delete the object prototype in the strict mode.
JAVASCRIPT CODE
<script>
   'use strict';
   let women = { name: "Aasha", age: 29 };
   delete women.prototype; // This is invalid
</script>
  1. Deleting the function using the delete operator is not allowed.
JAVASCRIPT CODE
<script>
   'use strict';
   function func() { }
   delete func; // This is invalid
</script>
  1. You can’t have a function with duplicate parameter values.
JAVASCRIPT CODE
<script>
   'use strict';
   function func(param1, param1, param2) {
      // Function with 2 param1 is not allowed!
   }
</script>
  1. You can’t assign octal numbers to variables.
JAVASCRIPT CODE
<script>
   'use strict';
   let octal = 010; // Throws an error
</script>
  1. You can’t use escape characters.
JAVASCRIPT CODE
<script>
   'use strict';
   let octal = \010; // Throws an error
</script>
  1. You can’t use reserved keywords like eval, arguments, public, etc., as an identifier.
JAVASCRIPT CODE
<script>
   'use strict';
   let public = 100; // Throws an error
</script>
  1. You can’t write to the readable property of the object.
JAVASCRIPT CODE
<script>
    'use strict';
    let person = {};

    Object.defineProperty(person, 'name', { value: "abc", writable: false });
    obj1.name = "JavaScript"; // throws an error
</script>
  1. You can’t assign values to the getters function.
JAVASCRIPT CODE
<script>
   'use strict';
   let person = { get name() { return "JavaScript"; } };
   obj1.name = "JavaScript"; // throws an error
</script>
  1. In the strict mode, when you use the β€˜this’ keyword inside the function, it refers to the reference object through which the function is invoked. If the reference object is not specified, it refers to the undefined value.
JAVASCRIPT CODE
<script>
   'use strict';
   function test() {
      console.log(this); // Undefined
   }
   test();
</script>
  1. You can’t use the β€˜with’ statement in the strict mode.
JAVASCRIPT CODE
<script>
   'use strict';
   with (Math) {x = sin(2)}; // This will throw an error
</script>
  1. You can’t use the eval() function to declare the variables for the security reason.
JAVASCRIPT CODE
<script>
   'use strict';
   eval("a = 8")
</script>
  1. You can’t use the keywords as an identifier that are reserved for the future. Below keywords are reserved for the future βˆ’
  • implements
  • interface
  • package
  • private
  • protected