string compare in javascript that returns a boolean

You can use the RegEx test() method which returns a boolean:

/a/.test('abcd'); // returns true.

You may use type augmentation, especially if you need to use this function often:

String.prototype.isMatch = function(s){
   return this.match(s)!==null 
}

So you can use:

var myBool = "ali".isMatch("Ali");

General view is that use of type augmentation is discouraged only because of the fact that it can collide with other augmentations.

According to Javascript Patterns book, its use must be limited.

I personally think it is OK, as long as you use a good naming such as:

String.prototype.mycompany_isMatch = function(s){
   return this.match(s)!==null 
}

This will make it ugly but safe.


there is .indexOf() which will return the position of the string found, or -1 if not found

Tags:

Javascript