Question: I am trying to optimize a function which does binary search of strings in JavaScript.

Binary search requires you to know whether the key is `==` the pivot or `<` the pivot.

But this requires two string comparisons in JavaScript, unlike in `C` like languages which have the `strcmp()` function that returns three values `(-1, 0, +1)` for (less than, equal, greater than).

Is there such a native function in JavaScript, that can return a ternary value so that just one comparison is required in each iteration of the binary search?

Answer: 
You can use the `localeCompare()` method. 

```
string_a.localeCompare(string_b);

/* Expected Returns:

 0:  exact match

-1:  string_a < string_b

 1:  string_a > string_b

 */

```

Further Reading:

* [MDN: String.prototype.localeCompare](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare)
* [Stack Overflow - Is there a JavaScript strcmp()?](https://stackoverflow.com/questions/1179366/is-there-a-javascript-strcmp)
* [Tutorials Point: JavaScript String - localeCompare() Method](http://www.tutorialspoint.com/javascript/string_localecompare.htm)
Well in JavaScript you can check two strings for values same as integers so yo can do this:

* `"A" < "B"`
* `"A" == "B"`
* `"A" > "B"`

And therefore you can make your own function that checks strings the same way as the `strcmp()`.

So this would be the function that does the same:

```
function strcmp(a, b)
{   
    return (a<b?-1:(a>b?1:0));  
}

```