Shrikrishna BHARDWAJ
Shrikrishna BHARDWAJ

Reputation: 43

What value is not strictly equal to itself & NaN in javascript

I need that value of a variable such that i get alert "Yes", in the code.

var a;

// set the value of a here ...
a=?

// don't edit the code below:
if (a === a) { 
    alert('No!'); 
} else {
    if (isNaN(a)) {
        alert('Not again!');
    }
    else { 
        alert('Yes');
    }
}

What is a?

Upvotes: 2

Views: 101

Answers (2)

Pranav
Pranav

Reputation: 2172

First, thanking @skbhardwaj.india for asking the question and thanking @torazaburo for answering the question.

Inspired by @torazaburo's answer, I would like to be independent of external/global variable hence using closures.

Object.defineProperty(window, 'a' , { get: (function(p){ return function(){ return p++; }})(0) });

Upvotes: 0

user663031
user663031

Reputation:

Consider the following:

n = 0;
Object.defineProperty(window, 'a', { get: function() { return n++; } });

> a === a
< false

Upvotes: 2

Related Questions