当我调试这部分代码时,我在控制台中看到:
当我尝试使用这两个变量构建条件时,它从不记录“你好”:
const token = localStorage.getItem('accesstoken');
const urlToken = this.getTokenFromUrl();
console.log('urlToken :', urlToken)
console.log('token :', token)
console.log('type of token :', typeof(token))
// if(!token && !urlToken){
// if((token && token !== null) && urlToken === undefined){
if (token === null && urlToken === undefined){
console.log('Hello');
我知道有时候 JS 很难处理这种情况。
我的问题是什么?
最佳答案
您的 token 是一个值为“null”的字符串。所以你的检查失败了。将其更改为 token === 'null'
将修复您的代码。
关于javascript - Ccombine test with null and undefined vars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51770920/