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

boolean - In javascript is -1 true or false? - Stack Overflow

programmeradmin0浏览0评论

I can't figure out if -1 is true or false in javascript, when I use indexOf.

let a =  'abc'.indexOf('abc');  
let b =  'def'.indexOf('abc');   

console.log(a);     // 0
console.log(b);     // -1
console.log(!a);    // true
console.log(!b);    // false

Why are the last two lines giving true/false?

From what I understand only == allows for type converting, since (=== is strict)

Is (!a) and (!b) using (==) internally somewhere?

I can't figure out if -1 is true or false in javascript, when I use indexOf.

let a =  'abc'.indexOf('abc');  
let b =  'def'.indexOf('abc');   

console.log(a);     // 0
console.log(b);     // -1
console.log(!a);    // true
console.log(!b);    // false

Why are the last two lines giving true/false?

From what I understand only == allows for type converting, since (=== is strict)

Is (!a) and (!b) using (==) internally somewhere?

Share Improve this question edited Jan 11, 2016 at 16:26 roro asked Jan 11, 2016 at 16:08 rorororo 2131 gold badge5 silver badges17 bronze badges 3
  • It is true (well, "truthy"). The only "falsey" number is zero. You need to test if it == -1 is true. – Blazemonger Commented Jan 11, 2016 at 16:10
  • 6 It's neither, it's a number not a boolean, however it is truthy – adeneo Commented Jan 11, 2016 at 16:10
  • It all started in c where 0 was false and everything else was true. JavaScript tried to follow this, but it got messy quickly without strict type enforcement. – Hogan Commented Jan 11, 2016 at 16:27
Add a ment  | 

4 Answers 4

Reset to default 7

From MDN:

In JavaScript, a truthy value is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN). (emphasis mine)

This means -1 is considered "truthy". You shouldn't be checking for "truthiness" directly on the value returned from indexOf anyway. -1 has a specific meaning in that the element you are looking for does not exist in the array. So it would make more sense to explicitly test against -1 using ===. To anyone reading the code, the intent is also much clearer than coercing the return value of indexOf and making your decision based on that.

To check what is certain variables conversion to boolean, the best trick is to use boolean operator to it. For example ! (negation) is boolean operator that gives you negative (true for false and vice versa) of that value.

And this knowledge is in Javascript (and also PHP and Python) used like this:

var booleanValue = !!someVariable;

That is in your case:

!!(-1) //returns true

So yes, -1 will convert to true by implicit javascript conversion.

In javascript, only "0" is false. When make a call with a preceeding "!", javascript will cast everything to boolean. Another interesting fact:

console.log(!!!a);    // false
console.log(!!!b);    // true

Thereby casting your "a" to a boolean with !! and getting the inverse value

-1 is a trule-like value. The only false-like value between numbers in JavaScript is 0. Other false-like values are '', NaN, undefined... I hope i didn't miss anything.

When using indexOf all you need to do is

if (someString.indexOf('value') >= -1) {
    //code if someString contains 'value'
}
发布评论

评论列表(0)

  1. 暂无评论