-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
Strict mode in JavaScript is a way to enforce a stricter set of rules for writing code. It helps catch common coding mistakes, prevents the use of certain unsafe features, and makes debugging easier. Introduced in ECMAScript 5, strict mode is not enabled by default. It can be applied globally for the entire script or locally within a function. Understanding strict mode is essential for writing clean, maintainable, and secure JavaScript code.
In this tutorial, you will learn about JavaScript strict mode, how to enable it, its benefits, practical examples, common mistakes, best practices, and real-world applications.
Strict mode is important because it:
Helps catch silent errors by turning them into exceptions
Prevents accidental creation of global variables
Disallows duplicate parameter names in functions
Disables features that are considered unsafe or deprecated
Improves performance in some JavaScript engines by allowing optimizations
Using strict mode ensures that your code behaves more predictably and reduces the risk of subtle bugs, especially in large projects or when collaborating with other developers.
You can enable strict mode for the entire script by placing "use strict"; at the top of the file.
"use strict";
var name = "Aarushi";
console.log(name);
All code in the script will now follow strict mode rules.
Strict mode can also be applied to a specific function by placing "use strict"; at the beginning of the function body.
function greet() {
"use strict";
var message = "Hello, Priya";
console.log(message);
}
greet();
This approach allows you to use strict mode selectively, without affecting the rest of the script.
Without strict mode, assigning a value to an undeclared variable automatically creates a global variable. Strict mode throws an error instead.
"use strict";
function test() {
age = 25; // ReferenceError: age is not defined
}
test();
this CoercionIn strict mode, this inside functions that are not called as methods is undefined, rather than the global object.
"use strict";
function showThis() {
console.log(this);
}
showThis(); // undefined
Strict mode prevents using the same parameter name multiple times in a function.
"use strict";
function sum(a, a, b) {
// SyntaxError: Duplicate parameter name not allowed in strict mode
return a + b;
}
Deleting a variable, function, or argument in strict mode results in a syntax error.
"use strict";
var name = "Saanvi";
delete name; // SyntaxError: Delete of an unqualified identifier in strict mode
Strict mode prohibits the use of octal numeric literals (numbers starting with 0).
"use strict";
var number = 010; // SyntaxError
Strict mode reserves certain keywords for future JavaScript versions. Using them as variable or function names will throw an error.
"use strict";
var private = "secret"; // SyntaxError
"use strict";
function calculate() {
total = 100; // ReferenceError
console.log(total);
}
calculate();
Without strict mode, total would silently create a global variable, potentially causing conflicts.
this Usage"use strict";
function showName() {
console.log(this.name);
}
showName(); // undefined instead of window.name
Strict mode ensures this is not implicitly coerced to the global object.
"use strict";
var obj = { name: "Aarushi" };
// delete obj; // SyntaxError
delete obj.name; // Works fine
You can delete object properties but not the variable itself.
"use strict";
function greet(name, name) {
// SyntaxError
return "Hello " + name;
}
Strict mode prevents confusion from duplicate parameters.
"use strict";
for (i = 0; i < 5; i++) {
console.log(i); // ReferenceError: i is not defined
}
Declaring let i = 0 solves the issue, ensuring loop variables remain local.
Forgetting "use strict"; at the top of scripts or functions
Expecting strict mode to apply to older scripts automatically
Declaring undeclared variables, which now throws errors
Using deprecated features that are disallowed in strict mode
Confusing global and local strict mode effects
Always use "use strict"; in modern JavaScript projects
Declare all variables explicitly with let, const, or var
Avoid duplicate function parameters and reserved keywords
Use strict mode inside modules, which are automatically in strict mode
Test legacy code thoroughly before enabling strict mode globally
Writing maintainable, error-free web applications
Preventing accidental global variables in large codebases
Enhancing security by enforcing safer coding practices
Ensuring compatibility with future JavaScript versions
Making debugging easier by catching errors early
JavaScript strict mode enforces a stricter set of rules for writing code, catching common mistakes, and preventing unsafe practices. It helps manage variable scope, prevents accidental globals, disallows duplicate parameters, and ensures predictable behavior of this. By enabling strict mode, either globally or locally, developers can write cleaner, more secure, and maintainable code. Following best practices ensures that applications remain robust and easier to debug.
Enable strict mode globally and try assigning a value to an undeclared variable. Explain the error.
Enable strict mode inside a function and try assigning a value to an undeclared variable within that function.
Write a function with duplicate parameter names in strict mode and observe the result.
Declare a constant and attempt to reassign its value in strict mode. Explain what happens.
Try deleting a built-in object or property (e.g., Object.prototype) in strict mode and explain the output.
Use this inside a standalone function in strict mode and log its value. Compare it with non-strict mode behavior.
Declare a variable using an octal literal (e.g., 010) in strict mode and observe the result.
Try using a reserved keyword (like static or public) as a variable name in strict mode. Explain why it fails.
Create a function in strict mode that accesses a global variable and another undeclared variable. Observe which causes an error.
Write a function in strict mode that works correctly with block-scoped variables using let and const.
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
