-
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 feature that helps developers write safer and cleaner code. By enabling strict mode, JavaScript enforces more rigorous error checking, preventing common mistakes and unsafe actions. It helps avoid silent errors, accidental global variables, and other pitfalls.
Strict mode is introduced in ECMAScript 5 (ES5) and can be applied globally or locally within functions.
You can enable strict mode for the entire script by placing "use strict";
at the top of the file:
"use strict";
let x = 10;
console.log(x); // 10
This applies strict mode to all code in the script.
Strict mode can also be applied to individual functions:
function strictFunction() {
"use strict";
y = 20; // ReferenceError: y is not defined
}
strictFunction();
Variables must be declared with var
, let
, or const
. Assigning to undeclared variables throws a ReferenceError in strict mode.
In normal mode, assigning a value to an undeclared variable creates a global variable. Strict mode prevents this:
"use strict";
x = 5; // ReferenceError: x is not defined
let x = 5; // Correct
Helps prevent accidental globals, which are common sources of bugs.
this
CoercionIn normal mode, this
inside a function defaults to the global object (window
in browsers). In strict mode, this
is undefined
for standalone functions:
"use strict";
function checkThis() {
console.log(this);
}
checkThis(); // undefined (instead of window)
Helps catch accidental references to global objects.
Strict mode disallows duplicate parameter names in functions:
"use strict";
function sum(a, a, b) { // SyntaxError
return a + a + b;
}
Prevents confusion and bugs in function logic.
Strict mode throws errors when you attempt to modify read-only properties, non-writable properties, or constants:
"use strict";
const PI = 3.14;
PI = 3.14159; // TypeError: Assignment to constant variable
Prevents silent failures in non-strict mode.
In strict mode, deleting variables, functions, or objects that cannot be deleted throws an error:
"use strict";
delete Object.prototype; // TypeError
Normal mode fails silently; strict mode alerts you to the mistake.
Octal number literals (e.g., 010
) are not allowed in strict mode:
"use strict";
let num = 010; // SyntaxError
Encourages using modern numeric representations like 0o10
.
Strict mode reserves certain keywords for future versions of JavaScript:
"use strict";
let public = 10; // SyntaxError
let static = 20; // SyntaxError
Prevents conflicts with future ECMAScript features.
"use strict";
function createUser() {
userName = "Alice"; // ReferenceError
let userAge = 25; // Correct
}
createUser();
Without strict mode, userName
would have become a global variable.
this
References"use strict";
function showThis() {
console.log(this);
}
showThis(); // undefined
Ensures this
is not automatically assigned to the global object.
"use strict";
function multiply(a, a) { // SyntaxError: duplicate parameter name
return a * a;
}
Helps avoid silent bugs and makes code predictable.
Always use "use strict";
at the top of scripts or functions to ensure safe coding.
Avoid relying on accidental globals; declare all variables explicitly.
Use let
and const
instead of var
to leverage block scoping.
Strict mode is particularly useful in large codebases to catch errors early.
Combine strict mode with ES6+ features like arrow functions and classes for modern, robust code.
Strict Mode enforces safer and cleaner JavaScript coding practices.
It prevents accidental globals, duplicate parameters, octal literals, unsafe assignments, and improper this
references.
Strict mode can be applied globally or locally within functions.
Using strict mode ensures predictable behavior, easier debugging, and fewer runtime errors.
Enabling strict mode is highly recommended for all modern JavaScript projects to maintain code quality and reliability.
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