function firstDiff(a, b) { var i = 0; while (a.charAt(i) === b.charAt(i)) if (a.charAt(i++) === '') return -1; return i; }
Returns the position at which the two lines a and b first differ, or -1 if they are equal.
A more effective but less readable version:
function firstDiff(a, b) { for (var i = 0, c; (c = a.charAt(i)) === b.charAt(i); ++i) if (c === '') return -1; return i; }
If you feel that you must first argue the arguments, then do this in a call:
firstDiff(toString(a), toString(b))
Most often it will be a waste of time. Know your details!
source share