“use strict” in Javascript

Enforce Strict Mode for Safer and More Reliable JavaScript Code

Rahul Jindal
3 min readFeb 28, 2023
Photo by Damian Zaleski on Unsplash

As JavaScript applications become more complex, it becomes crucial to ensure that the code written is secure, reliable, and maintainable. One of the tools available to JavaScript developers to enforce best practices is the “use strict” directive. In this blog, we will explore what the “use strict” directive is, how it works, and how it can help us write better JavaScript code.

What is “use strict” in JavaScript?

“Use strict” is a directive that allows developers to enable strict mode in their JavaScript code. Strict mode is a feature added in ECMAScript 5 that introduces a set of rules that make JavaScript code more secure, reliable, and maintainable. The “use strict” directive must be placed at the beginning of a JavaScript file or a function to enable strict mode.

  • Adding “use strict” on top of a file
'use strict';

// Your code goes here...
  • Adding “use strict” on top of a function
function myFunction() {
'use strict';

// Your code goes here...
}

How does “use strict” work?

When the “use strict” directive is enabled, JavaScript code is executed in strict mode, which introduces a set of rules that must be followed. Some of the key rules introduced by strict mode include:

  • Variable declarations are required: In strict mode, variables must be declared before they can be used. This prevents developers from accidentally creating global variables.
"use strict";

// using x without declaring it
x = 10;

// using x in a conditional statement
if (x > 5) {
console.log("x is greater than 5");
}

This will result in a ReferenceError being thrown.

  • Assigning to undeclared variables is not allowed: In strict mode, attempting to assign a value to an undeclared variable will result in a reference error. This prevents developers from accidentally creating global variables.
function example() {
'use strict';
x = 10; // ReferenceError: x is not defined
}

This results in a ReferenceError because x is not defined.

  • Deleting variables or functions is not allowed: In strict mode, attempting to delete a variable or function will result in a syntax error. This prevents developers from accidentally deleting variables or functions.
'use strict';

let myVar = 5;
console.log(myVar); // Output: 5

// Attempting to delete the variable
// generates a runtime error
delete myVar;
  • “this” keyword behaves differently: In strict mode, the value of “this” keyword is not implicitly set to the global object when it is not defined. This prevents developers from accidentally modifying the global object.
'use strict';

// Declare a global object
let myObj = {
prop: 'Hello',
greet: function() {
console.log(this.prop);
}
};

// Call the greet() method on myObj
myObj.greet(); // Output: Hello

// Create a new variable that refers to the greet() method
let myGreet = myObj.greet;

// Call the greet() method using the myGreet variable
myGreet(); // Output: TypeError: Cannot read property 'prop' of undefined

If we do not have used ‘use strict’ at top of the file, then this keyword would have take the reference of the global object and that would result in this.prop to be undefined.

  • Octal literals are not allowed: In strict mode, octal literals (numbers starting with a zero) are not allowed. This prevents developers from accidentally using octal literals, which can lead to unexpected behavior.
'use strict';

// Octal literal
let num = 0644; // SyntaxError: Octal literals are not allowed in strict mode.

// Decimal literal
let num2 = 644;

console.log(num2); // Output: 644

Benefits of using “use strict”

  1. Improving code quality: Strict mode rules help developers write better quality code by enforcing best practices and preventing common mistakes.
  2. Increasing security: Strict mode prevents developers from accidentally creating global variables or modifying the global object, which can lead to security vulnerabilities.
  3. Enhancing performance: Strict mode can help optimize JavaScript code by identifying potential performance issues and preventing unnecessary computations.
  4. Ensuring compatibility: Strict mode is supported in all modern browsers and JavaScript engines, ensuring that your code will work as expected across different platforms.

Conclusion

In conclusion, the “use strict” directive is a powerful tool that enables developers to enforce best practices. We hope this blog has provided you with a deeper understanding of what “use strict” is and how it can help you write better JavaScript code.

--

--