Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Can a === 1 && a === 2 && a === 3 be true in JavaScript?

  sonic0002        2018-04-06 12:17:29       5,261        2    

Lots of you may be aware that there is famous interview question which asks whether a == 1 && a == 2 && a == 3 can be true in JavaScript. And the answer to this question is YES. The reason is that == will do a non-strict comparison which will evaluate a to a number and this provides the possibility of dynamically return the value when every time a is accessed.

Have you ever wondered whether a === 1 && a === 2 && a === 3 can be true? At first glance, it seems this is impossible since === will do strict comparison and the type of the two sides must be the same before their values can be compared. And in this case, we have to ensure a to be a "number", hence we could not declare a as an object and override the valueOf() or toString() methods. 

  1. If Type(x) is different from Type(y), return false.
  2. If Type(x) is Number, then
    1. If x is NaN, return false.
    2. If y is NaN, return false.
    3. If x is the same Number value as y, return true.
    4. If x is +0 and y is -0, return true.
    5. If x is -0 and y is +0, return true.
    6. Return false.
  3. Return SameValueNonNumber(x, y).

However, the truth is that the above question can return a YES answer. Sören has provided a solution to this question in the comment of Can a == true && a == false be true in JavaScript?. In this post, we will try to explain a bit about the solution. To make the expression return true, another feature provided by JavaScript can be used -- Object.defineProperty(). The method Object.defineProperty() defines a new property directly on an object, or modifies an existing property on an object, and returns the object.

It has below syntax:

Object.defineProperty(obj, prop, descriptor)

We can define an attribute a on window object and then set its descriptor. The descriptor allows a group of options which can be applied to the property defined. Among them, the get option is really important here. According the MDN, the get option has following description:

A function which serves as a getter for the property, or undefined if there is no getter. When the property is accessed, this function is called without arguments and with this set to the object through which the property is accessed (this may not be the object on which the property is defined due to inheritance). The return value will be used as the value of the property.

Hence, we can define a as below:

let i = 1;
Object.defineProperty(window, 'a', {
    get: function(){
        return i++;
    }
});

With this created, every time when a is accessed, its value get incremented by 1. Hence the question in this post title will return a true value.

JAVASCRIPT  ===  STRICT COMPARISON 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  2 COMMENTS


Anonymous [Reply]@ 2022-10-18 04:26:46

Nice , but here is my small concern here:-

how can possible without define variable used in js

Ke Pi [Reply]@ 2022-10-21 20:54:41

sorry don't quite get u, can elaborate more?