DEV Community

Cover image for Understanding Strict Mode and Explicit Binding in JavaScript
Bart Zalewski
Bart Zalewski

Posted on

Understanding Strict Mode and Explicit Binding in JavaScript

In JavaScript, there are advanced features and techniques that can enhance the robustness and clarity of your code. In this post, we'll explore two important concepts: Strict Mode and Explicit Binding.

Strict Mode:

Strict Mode is a feature introduced in ECMAScript 5 that allows you to opt into a stricter set of rules and behaviors for JavaScript. When enabled, Strict Mode helps catch common coding mistakes and discourages the use of certain practices that are considered error-prone.

Here's how you can enable Strict Mode in your JavaScript code:

'use strict';
Enter fullscreen mode Exit fullscreen mode

Enabling Strict Mode at the beginning of a script or a function enables a set of strict rules for the entire script or function scope.

Some benefits of Strict Mode include:

  • Error Handling: Strict Mode catches common coding mistakes and throws errors that would otherwise be ignored in non-strict mode.
  • Restrictions on Reserved Words: It disallows the use of certain keywords as variable names, such as arguments, eval, and implements.
  • Avoids Global Variables: It prevents accidental creation of global variables by requiring variable declarations with var, let, or const.
  • Makes this Binding More Predictable: In non-strict mode, this can sometimes refer to the global object, leading to unexpected behavior. Strict Mode makes this binding more predictable by disallowing it to default to the global object in functions and methods.

Explicit Binding:

Explicit Binding refers to the ability to manually specify the value of this within a function or method. This is particularly useful when you need to control the context in which a function is executed.

JavaScript provides three methods for explicit binding: call, apply, and bind.

  • call: The call method allows you to call a function with a specified this value and arguments provided individually.
  • apply: The apply method is similar to call, but it accepts arguments as an array.
  • bind: The bind method creates a new function with the specified this value and arguments, allowing you to store it for later execution.

Here's an example demonstrating the use of call, apply, and bind:

const person = {
  name: 'John',
  greet: function(message) {
    return `${message}, ${}!`;

const alice = { name: 'Alice' };

console.log(, 'Hello'));
console.log(person.greet.apply(alice, ['Hi']));
const greetAlice = person.greet.bind(alice);
Enter fullscreen mode Exit fullscreen mode

Understanding and leveraging Strict Mode and Explicit Binding can help you write more reliable and maintainable JavaScript code. Whether you're working on large-scale applications or small projects, these concepts can make a significant difference in the quality of your code.

Top comments (0)