diff --git a/lib/buffer.js b/lib/buffer.js index 28534bab2783ad..497725516435de 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -580,9 +580,10 @@ function bidirectionalIndexOf(buffer, val, byteOffset, encoding, dir) { } else if (byteOffset < -0x80000000) { byteOffset = -0x80000000; } - byteOffset = +byteOffset; // Coerce to Number. - if (isNaN(byteOffset)) { - // If the offset is undefined, null, NaN, "foo", etc, search whole buffer. + // Coerce to Number. Values like null and [] become 0. + byteOffset = +byteOffset; + // If the offset is undefined, "foo", {}, coerces to NaN, search whole buffer. + if (Number.isNaN(byteOffset)) { byteOffset = dir ? 0 : (buffer.length - 1); } dir = !!dir; // Cast to bool.