Last Updated: September 09, 2019
·
6.217K
· shekhardesigner

Strict Mode & this keyword in JavaScript

Normally, in global scope this keyword refers to Window object. Eg:

<script>
console.log(this); //returns Window object.

//or

(function(){)
    console.log(this); //returns Window object
})();
</script>

HOWEVER, in JavaScript strict mode, this keyword in global scope is undefined. Eg:

<script>
(function(){
    "use strict";
    console.log(this); //will return *undefined*
})();
</script>

So use the strict mode wisely. Here you can read more fun things about JavaScript Strict Mode: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/Strict_mode

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside