Course contentsShow
← JavaScript
Lesson 547 of 2,72314. The 'this' KeywordFree lesson

Function Calls: When 'this' is Undefined (Strict Mode)

Understand how standalone function calls set 'this' to undefined in strict mode.

Function Calls: When 'this' is Undefined (Strict Mode)

You'll learn what happens to this when you call a function directly (not as a method) in strict mode.

The Core Idea

When you call a function as a standalone function—not attached to any object—JavaScript needs to decide what this should refer to. In strict mode, JavaScript makes a safer choice: it sets this to undefined instead of defaulting to the global object.

Think of it like calling a hotel concierge service without specifying which hotel you're calling from. In strict mode, the system says "I don't know which context you mean" (undefined) rather than guessing.

How It Works

'use strict';

function showThis() {
  console.log(this);
}

showThis(); // undefined

Notice there's no dot notation, no object before the function name. This is a plain function call, so this becomes undefined.

Compare this to a method call (which you learned in the previous lesson):

'use strict';

const obj = {
  showThis: function() {
    console.log(this);
  }
};

obj.showThis(); // logs obj (the object before the dot)

Why This Matters

Strict mode prevents accidental bugs. If you try to access properties on this in a standalone function call, you'll get an error immediately rather than silently creating global variables (which happens in non-strict mode).

'use strict';

function dangerousFunction() {
  this.newProperty = 'oops'; // TypeError: Cannot set property of undefined
}

dangerousFunction();

Key Takeaway: In strict mode, calling a function directly (without an object) sets this to undefined, protecting you from accidentally referencing the wrong context.