diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp index cd414e4ae0c..98204761f0e 100644 --- a/Userland/Libraries/LibJS/AST.cpp +++ b/Userland/Libraries/LibJS/AST.cpp @@ -477,6 +477,8 @@ Value ForInStatement::execute(Interpreter& interpreter, GlobalObject& global_obj auto rhs_result = m_rhs->execute(interpreter, global_object); if (interpreter.exception()) return {}; + if (rhs_result.is_nullish()) + return {}; auto* object = rhs_result.to_object(global_object); while (object) { auto property_names = object->get_enumerable_own_property_names(Object::PropertyKind::Key); diff --git a/Userland/Libraries/LibJS/Tests/loops/for-in-basic.js b/Userland/Libraries/LibJS/Tests/loops/for-in-basic.js index 5a35ee1c91a..ee60bd5eb8f 100644 --- a/Userland/Libraries/LibJS/Tests/loops/for-in-basic.js +++ b/Userland/Libraries/LibJS/Tests/loops/for-in-basic.js @@ -38,6 +38,12 @@ test("iterate through object", () => { expect(a).toEqual(["a", "b", "c"]); }); +test("iterate through undefined", () => { + for (const property in undefined) { + expect.fail(); + } +}); + test("use already-declared variable", () => { var property; for (property in "abc");