diff --git a/lib/internal/url.js b/lib/internal/url.js index 446c22f5b66067..be20127b45f191 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -337,9 +337,9 @@ class URLSearchParams { constructor(init = undefined) { markTransferMode(this, false, false); - if (init == null) { + if (init === undefined) { // Do nothing - } else if (typeof init === 'object' || typeof init === 'function') { + } else if (init !== null && (typeof init === 'object' || typeof init === 'function')) { const method = init[SymbolIterator]; if (method === this[SymbolIterator] && #searchParams in init) { // While the spec does not have this branch, we can use it as a diff --git a/test/parallel/test-whatwg-url-custom-searchparams-constructor.js b/test/parallel/test-whatwg-url-custom-searchparams-constructor.js index 75888f9270d25b..9cfbd03a9737f2 100644 --- a/test/parallel/test-whatwg-url-custom-searchparams-constructor.js +++ b/test/parallel/test-whatwg-url-custom-searchparams-constructor.js @@ -28,8 +28,14 @@ function makeIterableFunc(array) { let params; params = new URLSearchParams(undefined); assert.strictEqual(params.toString(), ''); + // Per WebIDL union resolution, null is coerced to the USVString "null". + // Refs: https://url.spec.whatwg.org/#interface-urlsearchparams params = new URLSearchParams(null); - assert.strictEqual(params.toString(), ''); + assert.strictEqual(params.toString(), 'null='); + params = new URLSearchParams(false); + assert.strictEqual(params.toString(), 'false='); + params = new URLSearchParams(0); + assert.strictEqual(params.toString(), '0='); params = new URLSearchParams( makeIterableFunc([['key', 'val'], ['key2', 'val2']]) );