Skip to main content
improved formatting
Source Link
Taki
  • 17.7k
  • 5
  • 28
  • 48

Kevins answer is correct but it requires a "magic" number as follows: var containsChar = s.indexOf(somechar) !== -1;

var containsChar = s.indexOf(somechar) !== -1; 

In that case you need to know that -1 stands for not found. I think that a bit better version would be: var containsChar = s.indexOf(somechar) >= 0;

var containsChar = s.indexOf(somechar) >= 0; 

Kevins answer is correct but it requires a "magic" number as follows: var containsChar = s.indexOf(somechar) !== -1; In that case you need to know that -1 stands for not found. I think that a bit better version would be: var containsChar = s.indexOf(somechar) >= 0;

Kevins answer is correct but it requires a "magic" number as follows:

var containsChar = s.indexOf(somechar) !== -1; 

In that case you need to know that -1 stands for not found. I think that a bit better version would be:

var containsChar = s.indexOf(somechar) >= 0; 
Source Link

Kevins answer is correct but it requires a "magic" number as follows: var containsChar = s.indexOf(somechar) !== -1; In that case you need to know that -1 stands for not found. I think that a bit better version would be: var containsChar = s.indexOf(somechar) >= 0;