Can ‘this’ ever be null in Javascript

In non-strict mode, this has undergone an Object(this) transformation, so it’s always truthy. The exceptions are null and undefined which map to the global object. So this is never null and always truthy, making both checks superfluous.

In strict mode, however, this can be anything so in that case you’d have to watch out. But then again you have to opt in for strict mode yourself, so if you don’t do that there are no worries.

(function() {               return this; }).call(null); // global object
(function() { "use strict"; return this; }).call(null); // null

The specification of ES5 says:

The thisArg value is passed without modification as the this value. This is a change from Edition 3, where a undefined or null thisArg is replaced with the global object and ToObject is applied to all other values and that result is passed as the this value.

Leave a Comment

techhipbettruvabetnorabahisbahis forumueduedueduseduedusedusedueduedusedus