Example 1 — this in functions
Bare calls get undefined this.
JavaScript
"use strict";
function f(){ return this; }
console.log(f());How It Works
- Not the global object in strict mode.
Strict mode opts into a safer subset of JavaScript: assigning undeclared variables throws, this is undefined in bare calls, and some footguns are disabled. ES modules and modern class bodies are strict by default.
"use strict";
try {
undeclared = 1;
} catch (err) {
console.log(err.name);
}Bare calls get undefined this.
"use strict";
function f(){ return this; }
console.log(f());Illegal in strict mode.
console.log("function (a,a){} is SyntaxError in strict");No pragma needed.
console.log("type=module enables strict automatically");CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
6 people found this page helpful