- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - Strict Mode
Strict Mode in JavaScript
In JavaScript, the strict mode is 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 −
'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 the error in the console and doesn't print the output.
In short, the strict mode doesn't allow you to use the variable without its declaration.
<html> <head> <title> Using the strict mode gloablly </title> </head> <body> <script> "use strict"; let y = 50; // This is valid document.write("The value of the X is: " + y); x = 100; // This is not valid document.write("The value of the X is: " + x); </script> </body> </html>
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.
<html> <head> <title> Using the strict mode gloablly </title> </head> <body> <script> x = 100; // This is valid document.write("The value of the X is - " + x); function test() { "use strict"; y = 50; // This is not valid document.write("The value of the y is: " + x); } test(); </script> </body> </html>
Mistakes that you should't make in the strict mode
1. You can't initialize the variable with a value without declaring it.
<script> 'use strict'; num = 70.90; // This is invalid </script>
2. Similarly, you can't use the object without declaring it.
<script> 'use strict'; numObj = {a: 89, b: 10.23}; // This is invalid </script>
3. You can't delete objects using the delete keyword.
<script> 'use strict'; let women = { name: "Aasha", age: 29 }; delete women; // This is invalid </script>
4. You can't delete the object prototype in the strict mode.
<script> 'use strict'; let women = { name: "Aasha", age: 29 }; delete women.prototype; // This is invalid </script>
5. Deleting the function using the delete operator is not allowed.
<script> 'use strict'; function func() { } delete func; // This is invalid </script>
6. You can't have a function with duplicate parameter values.
<script> 'use strict'; function func(param1, param1, param2) { // Function with 2 param1 is not allowed! } </script>
7. You can't assign octal numbers to variables.
<script> 'use strict'; let octal = 010; // Throws an error </script>
8. You can't use escape characters.
<script> 'use strict'; let octal = \010; // Throws an error </script>
9. You can't use reserved keywords like eval, arguments, public, etc., as an identifier.
<script> 'use strict'; let public = 100; // Throws an error </script>
10. You can't write to the readable property of the object.
<script> 'use strict'; let person = {}; Object.defineProperty(person, 'name', { value: "abc", writable: false }); obj1.name = "JavaScript"; // throws an error </script>
11. You can't assign values to the getters function.
<script> 'use strict'; let person = { get name() { return "JavaScript"; } }; obj1.name = "JavaScript"; // throws an error </script>
12. 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.
<script> 'use strict'; function test() { console.log(this); // Undefined } test(); </script>
13. You can't use the 'with' statement in the strict mode.
<script> 'use strict'; with (Math) {x = sin(2)}; // This will throw an error </script>
14. You can't use the eval() function to declare the variables for the security reason.
<script> 'use strict'; eval("a = 8") </script>
15. 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