2

general javascript question please.

I have a button which will flag whether a user wants to share a post to facebook or not. When clicked, it will check if a user already has a facebook token or not. And also, whether the token is valid.

If either is not true, it will prompt the user to signin to facebook.

Right now the code works just fine. But for a long time I was doing if(facebookToken === null)

and it was ignoring it. Only until I switched to == did it work.

Why is this? I'm console logging it and it was coming back as null

This is my code that works:

  shareToFacebookPress() {
    const timeNow = Date.now();
    const facebookToken = this.props.facebook_token;
    console.log(facebookToken);
    const facebookExpiry = this.props.facebook_token_expiry * 1000;
    console.log(facebookExpiry);
    if (facebookToken == null || facebookExpiry < timeNow) {
      console.log("doesn't exist");
      this.props.signInFacebook();
    } else {
      this.state.shareToFacebook ?
      this.setState({ shareToFacebook: false }) :
      this.setState({ shareToFacebook: true });
    }
  }

Surely === direct comparison, so if I’m console loggin the value and getting back null, surely it if(value === null) should equate to true?

This seems to be an impossibility. What’s happening here?

if value console logs to null and I do if(value === null) why doesn’t that equate to true?

Surely if it’s console logging null then it has to be it?

It’s not coming back as "null", which would make sense if I was using ==

bloppit
  • 621
  • 8
  • 22
  • 2
    Are you sure it was `null`? IIRC, the only values when comparing `value == null` gives `true` are `null` and `undefined`. – Rafael Jan 14 '17 at 00:09
  • yup 100% There's zero record of it within the database and as such it's console logging `null` – bloppit Jan 14 '17 at 00:11
  • 1
    null === undefined // false null == undefined // true – Mirko Vukušić Jan 14 '17 at 00:11
  • Also, while `null == undefined` is `true`, `null === undefined` is `false`. `null === null` is definitely supposed to be `true` ([MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null)). – jsfan Jan 14 '17 at 00:11
  • http://stackoverflow.com/questions/18808226/why-is-typeof-null-object – borracciaBlu Jan 14 '17 at 00:17
  • 3
    Please do `console.log(typeof facebookToken, facebookToken, !!facebookToken, facebookToken === null, facebookToken == null, facebookToken === void 0, facebookToken == void 0)` and post the results here. – Oriol Jan 14 '17 at 00:30
  • undefined null false false true true true How can it be undefined if it's console logging as `null`? – bloppit Jan 14 '17 at 00:36
  • What browser/version are you using? – Niko Bellic Jan 14 '17 at 00:45
  • it's react native. I'm using xcodes simulator – bloppit Jan 14 '17 at 00:46
  • sorry, I don't understand @RobG, it needs to exist AND be valid – bloppit Jan 14 '17 at 00:47
  • I'm not familiar with React Native, but I'm guessing there's a bug in its JavaScript engine/implementation. If facebookToken is null, the console.log posted by @Oriol should return "object null false true true false true" ... If facebookToken is undefined, the log should return "undefined undefined false false true true true" ... You're getting something different and very strange indeed. – Niko Bellic Jan 14 '17 at 00:56
  • 1
    @bloppit It seems the console displays undefined as null. Try `console.log(undefined)`. Is this on a web browser? Try another one. – Oriol Jan 14 '17 at 01:01

1 Answers1

-1

null printed by console.log(null) is by itself just a literal expression, per MDN description of null. null is not a property of a global object, like value that you mentioned above, but rather an object. what you are doing above with ===, actually, is comparing an object with a value(or property). Thus the type of value and null is different, and this causes if(value === null) to turn out to be false. However, when using == to check if(value == null), a type-conversion is performed before checking. So value and null hold the same type just before checking, allowing you to compare value and null. One thing to note, though, is that == is very lenient in its checking of equality, and is easy to make a mistake with.

Jin
  • 92
  • 3
  • Once the types are made equal using the rules in the [*Abstract Equality Comparison* algorithm](http://ecma-international.org/ecma-262/7.0/index.html#sec-abstract-equality-comparison), the evaluation is identical to that of the [*Strict Equality Comparison algorithm*](http://ecma-international.org/ecma-262/7.0/index.html#sec-strict-equality-comparison). – RobG Jan 14 '17 at 00:42
  • -1, sorry. You seem to have misunderstood the term "type". The type of `null` is `object`. Syntactically, `null` is a literal, but that literal denotes a value, and it's absolutely possible for a property to take that value. (For example, after running `window.foo = null`, `window.foo === null` will be `true`.) – ruakh Jan 14 '17 at 01:49
  • I see that you've edited your answer, but it's still wrong, for the same reason. – ruakh Jan 14 '17 at 21:45