Logo

Programming-Idioms

History of Idiom 39 > diff from v27 to v28

Edit summary for version 28 by programming-idioms.org:
[JS] Better declare new variable ok

Version 27

2016-11-11, 19:59:49

Version 28

2016-11-11, 20:01:51

Idiom #39 Check if string contains a word

Set boolean ok to true if string word is contained in string s as a substring, or to false otherwise.

Idiom #39 Check if string contains a word

Set boolean ok to true if string word is contained in string s as a substring, or to false otherwise.

Code
ok = s.indexOf(word) !== -1;
Code
var ok = s.indexOf(word) !== -1;
Comments bubble
indexOf returns -1 if the word isn't found.
Comments bubble
indexOf returns -1 if the word isn't found.