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 ==