Embracing Modern JavaScript: A Guide to Writing Code with “use strict”

Embracing Modern JavaScript: A Guide to Writing Code with “use strict”

In the ever-evolving landscape of JavaScript development, keeping up with modern best practices is essential for writing clean, efficient, and secure code. One such practice is using the "use strict" directive, which helps enforce stricter parsing and error handling of your JavaScript code. This guide will walk you through the benefits of "use strict" and demonstrate how to incorporate it into your JavaScript projects.

What is “use strict”?

The "use strict" directive was introduced in ECMAScript 5 (ES5) to allow developers to opt into a “strict mode” that catches common coding errors and unsafe actions. When enabled, it changes the JavaScript runtime behavior to be more strict about certain actions, helping developers avoid mistakes that could lead to bugs.

Benefits of Using “use strict”

Prevents the Use of Undeclared Variables: Without strict mode:

x = 10; // This will create a global variable

With strict mode:

"use strict";
x = 10; // This will throw an error: "x is not defined"

Eliminates this Pitfalls: In non-strict mode, this inside a function called as a standalone function refers to the global object. In strict mode, this is undefined in such cases:

function showThis() {
    "use strict";
    console.log(this); // undefined, instead of the global object
}
showThis();

Disallows Duplicate Property Names or Parameter Values:

"use strict";
let obj = {
    prop: 1,
    prop: 2 // Will throw an error: Duplicate data property in object literal
};

Prevents Assignment to Read-Only Properties:

"use strict";
let obj = {};
Object.defineProperty(obj, "readOnlyProp", {
    value: "fixed",
    writable: false
});
obj.readOnlyProp = "modified"; // Will throw an error: Cannot assign to read-only property

How to Enable “use strict”

1. Globally Inside a Script

You can enable strict mode for an entire script by placing "use strict"; at the top of your JavaScript file. This will enforce strict mode for the entire file.

"use strict";

function myFunction() {
    return "Hello World!";
}

2. Within Individual Functions

You can also enable strict mode within specific functions. This method is useful if you only want to enforce strict mode on certain parts of your code.

function myFunction() {
    "use strict";
    return "Hello World!";
}

Example: Writing a “Hello World” Program with “use strict”

Let’s revisit our “Hello World” program from a previous example, this time incorporating "use strict" to ensure any common errors are caught early.

Step 1: Create an HTML File

Create a new index.html file and add the following code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World in JavaScript</title>
</head>
<body>
    <script src="hello.js"></script>
</body>
</html>

Step 2: Create a JavaScript File

Create a new hello.js file with the following content:

"use strict";

console.log('Hello, World!');

Step 3: Open the HTML File in a Web Browser

Open index.html in a web browser and check the console (usually opened with F12 or right-clicking on the page and selecting “Inspect” -> “Console”). You should see the message “Hello, World!” printed without any errors.

Conclusion

Using "use strict" is a simple yet powerful way to improve the quality and security of your JavaScript code. By catching common mistakes and enforcing modern coding standards, it helps you write more reliable and maintainable code. As you continue your journey in JavaScript development, adopting such best practices will serve as a strong foundation for building robust applications.


By integrating "use strict" into your JavaScript development workflow, you’re taking an important step toward writing more secure, maintainable, and error-free code. Embrace these modern practices to enhance your skills and build better applications. Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *