JavaScript

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 Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript Strict Mode


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.

Why Strict Mode Is Important

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.

How to Enable Strict Mode

Global Strict Mode

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.

Local Strict Mode

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.

Features of Strict Mode

1. Prevents Accidental Global Variables

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();

2. Eliminates this Coercion

In 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

3. Disallows Duplicate Parameter Names

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;
}

4. Throws Error on Deleting Variables

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

5. Disallows Octal Literals

Strict mode prohibits the use of octal numeric literals (numbers starting with 0).

"use strict";

var number = 010; // SyntaxError

6. Reserved Keywords

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

Practical Examples

Example 1: Catching Typo Errors

"use strict";

function calculate() {
    total = 100; // ReferenceError
    console.log(total);
}

calculate();

Without strict mode, total would silently create a global variable, potentially causing conflicts.

Example 2: Preventing Unsafe 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.

Example 3: Safe Deletion

"use strict";

var obj = { name: "Aarushi" };
// delete obj; // SyntaxError
delete obj.name; // Works fine

You can delete object properties but not the variable itself.

Example 4: Avoiding Duplicate Parameters

"use strict";

function greet(name, name) {
    // SyntaxError
    return "Hello " + name;
}

Strict mode prevents confusion from duplicate parameters.

Example 5: Preventing Accidental Globals in Loops

"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.

Common Mistakes

  • 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

Best Practices

  • 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

Real-World Applications

  • 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

Summary of JavaScript Strict Mode

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.


Practice Questions

  1. Enable strict mode globally and try assigning a value to an undeclared variable. Explain the error.

  2. Enable strict mode inside a function and try assigning a value to an undeclared variable within that function.

  3. Write a function with duplicate parameter names in strict mode and observe the result.

  4. Declare a constant and attempt to reassign its value in strict mode. Explain what happens.

  5. Try deleting a built-in object or property (e.g., Object.prototype) in strict mode and explain the output.

  6. Use this inside a standalone function in strict mode and log its value. Compare it with non-strict mode behavior.

  7. Declare a variable using an octal literal (e.g., 010) in strict mode and observe the result.

  8. Try using a reserved keyword (like static or public) as a variable name in strict mode. Explain why it fails.

  9. Create a function in strict mode that accesses a global variable and another undeclared variable. Observe which causes an error.

  10. Write a function in strict mode that works correctly with block-scoped variables using let and const.


Try a Short Quiz.

No quizzes available.


JavaScript

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 Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top