indexOf empty string is zero. why?
问题 why this is happening in javascript? 'abc'.indexOf('a'); //0 'abc'.indexOf(''); //0 while in the other falsy values, the value is -1: 'abc'.indexOf(); //-1 'abc'.indexOf(false); //-1 'abc'.indexOf(undefined); //-1 i have seen also those questions: Q1 didnt understand the answer in depth. Q2 in java and not javascript. 回答1: The answer, fundamentally, is: Because that's how the function is specified to behave. And it makes sense, from a certain perspective. The main bit related to returning 0