How to Fix “Cannot convert undefined or null to object” in JavaScript
When the browser console shows TypeError: Cannot convert undefined or null to object, you have passed null or undefined to Object.keys or Object.entries. It happens when you list the object in settings saved to localStorage or in JSON received from an API, and the value has not been saved yet or the field does not exist. Replacing the value with an empty object using ?? {} before passing it in removes the error.
In my test environment, I built a page that displays saved settings, and I pressed the button with nothing saved, which caused this error. I copied the printed message exactly as it appeared, and I confirmed that the error disappears after the fix.
The test environment was Ubuntu 24.04 with Node.js 22.23.4. The page was served by Python 3.11's http.server. The browser was Chromium 153, launched with Playwright 1.63.0. I clicked the button with Playwright and read the console messages through the DevTools protocol. I also took screenshots of the DevTools panel. I did not test the messages in Firefox or Safari, or use of this in frameworks such as React.
Object.entries throws when no settings are saved
The page contains only a button and a paragraph to show the result.
<button id="show">保存した設定を表示</button>
<p id="result">(まだ表示していません)</p>
<script type="module" src="main.js"></script>
main.js reads the settings saved as a JSON string under settings in localStorage, and lists them as "name: value" using Object.entries.
const button = document.getElementById('show');
const result = document.getElementById('result');
button.addEventListener('click', () => {
const settings = JSON.parse(localStorage.getItem('settings'));
const rows = Object.entries(settings).map(([key, value]) => `${key}:${value}`);
result.textContent = rows.join(' / ');
});
After saving {"theme":"dark","fontSize":"16px"} to settings and pressing the button, the page showed "theme:dark / fontSize:16px". However, when I pressed the button with nothing saved, the page stayed at "(not displayed yet)", and the DevTools Console panel showed the following.
Uncaught TypeError: Cannot convert undefined or null to object
at Object.entries (<anonymous>)
at HTMLButtonElement.<anonymous> (main.js:6:23)

The message means "cannot convert undefined or null to an object." The second line, at Object.entries (<anonymous>), shows which function received the bad value. The third line, main.js:6:23, is line 6, column 23 of main.js. That column is the position of entries in Object.entries, counting the two leading spaces.
Where the null comes from
According to MDN's description of Storage.getItem(), getItem returns null when no key with the given name exists. In the test Chromium, getItem on a key that had never been saved also returned null.
Passing this null to JSON.parse does not throw. In my test environment, JSON.parse(null) returned null. So settings reached Object.entries still as null, and that is where the error occurred. The line where the error appears is different from the line where the value became null, so I traced back from the function name on line 2 of the message and the position on line 3 to find the variable that was passed.
MDN's description of Object.entries() states that values that are not objects are converted to objects before use, and that undefined and null cannot be converted, so TypeError is thrown at that point. The description of Object.keys() says the same.
Fix it by replacing with an empty object using ?? {}
On line 5, I made the code use an empty object {} when the result of JSON.parse is null or undefined. I also added a display for when the list is empty.
const settings = JSON.parse(localStorage.getItem('settings')) ?? {};
const rows = Object.entries(settings).map(([key, value]) => `${key}:${value}`);
result.textContent = rows.length > 0 ? rows.join(' / ') : '保存した設定はありません';
When I pressed the button with nothing saved, no error occurred, and the page showed "No saved settings". Nothing appeared in the DevTools Console panel. When settings were saved, the output was the same as before the fix: "theme:dark / fontSize:16px".
As described in MDN's explanation of the nullish coalescing operator, ?? returns the right-hand value only when the left-hand value is null or undefined.
A missing JSON field also causes the same error with undefined
I also tested passing an object from JSON received from an API to Object.keys. In the test, instead of a real API, I read users/<number>.json from the same server with fetch to get user data. From that, I list the names in the notification settings (notifications). User 1 has notifications, and user 2 does not. The first two lines of main.js are the same as before, so I have omitted them.
{"name":"たま","notifications":{"mail":true,"push":false}}
{"name":"みけ"}
button.addEventListener('click', async () => {
const id = new URLSearchParams(location.search).get('id');
const user = await (await fetch(`users/${id}.json`)).json();
const keys = Object.keys(user.notifications);
result.textContent = `${user.name} の通知設定:${keys.join('、')}`;
});
For user 1 (Tama), the page showed "Tama's notification settings: mail, push". I opened the page with ?id=2 in the URL and pressed the button for user 2 (Mike). The page stayed the same, and the Console panel showed the following.
Uncaught (in promise) TypeError: Cannot convert undefined or null to object
at Object.keys (<anonymous>)
at HTMLButtonElement.<anonymous> (main.js:7:23)
Since the field does not exist, user.notifications is undefined. The message text is the same as for null, so the message alone does not tell you which one it was. Because this happened inside an async function, the message begins with Uncaught (in promise). The second line now reads at Object.keys.
I fixed this with ?? {} as well.
const keys = Object.keys(user.notifications ?? {});
result.textContent = `${user.name} の通知設定:${keys.length > 0 ? keys.join('、') : 'なし'}`;
For user 2 (Mike), no error occurred either, and the page showed "Mike's notification settings: none". The display for user 1 did not change.
When the value passed to Object.keys or Object.entries can be null or undefined because nothing was saved or a field is missing, replace it with an empty object using ?? {} before passing it in.
Which calls throw and which do not
These are the results of passing various values in Chromium.
| Expression | Result |
|---|---|
Object.keys(undefined) |
TypeError: Cannot convert undefined or null to object |
Object.keys(null) |
Same error |
Object.entries(null) |
Same error |
Object.values(undefined) |
Same error |
Object.assign(null, { a: 1 }) |
Same error |
Object.assign({}, null) |
No error, returns {} |
({ ...null }) |
No error, returns {} |
for (const k in null) |
No error, the loop does not run |
Object.keys('abc') |
["0","1","2"] |
Object.keys(0), Object.keys(false) |
[] |
Object.values and passing null as the first argument of Object.assign also produced the same message. Passing null as the second or later argument to Object.assign, using it in an object spread, or using it in for...in did not throw. Strings, numbers, and booleans are converted to objects as MDN describes, so they do not throw either. A string returned its character indexes, and a number or boolean returned an empty array.
Running Object.entries(null) in Node.js 22 also produced TypeError: Cannot convert undefined or null to object, with the same text as in Chromium.




