最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Boolean operators in if statement not working - Stack Overflow

programmeradmin0浏览0评论

Could someone explain what's happening here? I'm trying to use the javascript !! (double-bang) operator as described here in conjunction with HTML5 local storage (I'm storing 0 and 1 values and testing for truthiness, but I also need a missing key to be false, hence the undefined at the start.

Although it echos to the console as false when typecast, it doesn't in the 'if' statement.

var foo = undefined;

// outputs undefined
console.log(foo)

// typecast to non-inverted boolean 
console.log(!!foo);

if (!!foo) {
    console.log("If was false before, why won't this execute?");
}​ else {
    console.log("It didn't work");
}​​​​​​​​​​​​​​​

Produces:

undefined
false
It didn't work 

/
(Chrome v 23.0.1271.97 & Firefox 16.0.1, OS X 10.8.2)

Edit - corrected code:

(The previous 'if' statement was just evaluating as false, so that branch would never run.)

var foo = false;

// outputs undefined
console.log(foo)

// typecast to non-inverted boolean 
console.log(!!foo);

if (!!foo == false) {
    console.log("Matches for foo undefined, foo = 0 and foo = false");
} else {
    console.log("Matches for foo = 1 and foo = true");
}​

/

Could someone explain what's happening here? I'm trying to use the javascript !! (double-bang) operator as described here in conjunction with HTML5 local storage (I'm storing 0 and 1 values and testing for truthiness, but I also need a missing key to be false, hence the undefined at the start.

Although it echos to the console as false when typecast, it doesn't in the 'if' statement.

var foo = undefined;

// outputs undefined
console.log(foo)

// typecast to non-inverted boolean 
console.log(!!foo);

if (!!foo) {
    console.log("If was false before, why won't this execute?");
}​ else {
    console.log("It didn't work");
}​​​​​​​​​​​​​​​

Produces:

undefined
false
It didn't work 

http://jsfiddle/YAAA7/
(Chrome v 23.0.1271.97 & Firefox 16.0.1, OS X 10.8.2)

Edit - corrected code:

(The previous 'if' statement was just evaluating as false, so that branch would never run.)

var foo = false;

// outputs undefined
console.log(foo)

// typecast to non-inverted boolean 
console.log(!!foo);

if (!!foo == false) {
    console.log("Matches for foo undefined, foo = 0 and foo = false");
} else {
    console.log("Matches for foo = 1 and foo = true");
}​

http://jsfiddle/YAAA7/1/

Share Improve this question edited May 23, 2017 at 12:24 CommunityBot 11 silver badge asked Dec 17, 2012 at 19:05 William TurrellWilliam Turrell 3,3487 gold badges41 silver badges59 bronze badges 5
  • 1 Well, !!foo is false, as you say, so the else branch will be taken. This is how it is supposed to work. – Frédéric Hamidi Commented Dec 17, 2012 at 19:08
  • In one of the answer's ments, someone noted, correctly, that '!!' is not an operator--it's two '!' operators. – AlexMA Commented Dec 17, 2012 at 19:10
  • It's the oposite of the oposite of what the value of the variable. – Elias Van Ootegem Commented Dec 17, 2012 at 19:17
  • @AlexMA In that case would say it's better practice to write Boolean(foo) instead of !!foo? – William Turrell Commented Dec 17, 2012 at 19:26
  • 1 @williamt Yes, it's probably better practice if the code is likely to be edited by those who aren't familiar with the notation. But some programmers enjoy throwing in a stylistic flourish once in a while, especially if it reduces the number of characters. – AlexMA Commented Dec 18, 2012 at 3:18
Add a ment  | 

2 Answers 2

Reset to default 5

That is expected behavior. If you double not false, you get false. This ,!l is a not operator. This ,!!, is two not operators. The double not operator is sometimes used to cast to the Boolean type.

! false === true
!! false === false
var foo = undefined;

foo will return true. Because, in JavaScript "", undefined, 0, NaN, false and null are considered falsey values.

http://www.mapbender/JavaScript_pitfalls:_null,_false,_undefined,_NaN .

From your code:

var foo = undefined; //false

console.log(!!foo); // !!foo = false;

if(!!foo) { //false
  console.log("It will not e because the condition fails");
}else{
    console.log("Else Part");
 }
发布评论

评论列表(0)

  1. 暂无评论