How to tell if one string is one edit away from another string in JavaScript

Here is one way to tell if a string is one edit (insertion, removal, modification) from another string in JavaScript.
Space complexity: O(1)
Time complexity: O(n) to iterate through n characters

oneAwayTry in REPL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function oneAway(string1, string2) {
if (string1.length - string2.length > 1) return false;
let numDiff = 0;
if (string1.length === string2.length) {
for (let i = 0; i < string1.length; i++) {
const char1 = string1[i];
const char2 = string2[i];
if (char1 !== char2) numDiff++;
}
} else {
for (let i = 0, j = 0; i < Math.max(string1.length, string2.length) && j < Math.max(string1.length, string2.length); i++, j++) {
if (string1[i] !== string2[j]) {
numDiff++;
if (numDiff > 1) return false;
(string1.length > string2.length && i++) || j++;
}
const char1 = string1[i];
const char2 = string2[j];
if (char1 !== char2) numDiff++;
}
}
return numDiff <= 1;
}

Test cases can be found at REPL.it