JavaScript TypeError: Cannot set properties of null
A plain-words explanation of this JavaScript message: what it means, the usual causes, and the steps that fix it.
The messageUncaught TypeError: Cannot set properties of null (setting 'textContent')
Make the error yourself
Run in Google Chrome 157.0.8103.51. This is the smallest example we could find that shows it.
JavaScript (in a script tag)
document.getElementById("mesage").textContent = "Hello";Browser console (the real message)
TypeError: Cannot set properties of null (setting 'textContent')Why it happens
document.getElementById and querySelector return null when no element matches. Then writing to null.textContent fails. The example has a typo in the id (mesage). The other very common cause is timing: the script runs before the browser has built the element, because the <script> tag comes before the element in the page. A third cause is a class or id that exists only on some pages.
How to fix it
- Check the id or selector against the HTML, letter by letter. Ids are case sensitive.
- Put the
<script>at the end of<body>, or add thedeferattribute to it, so the page exists when the code runs. - Or wait:
document.addEventListener("DOMContentLoaded", () => { ... }). - Test before use:
if (el) { ... }orel?.classList.add("on"). - In the browser console run
document.querySelector("#message"). If you getnull, the selector is wrong or the element is not there yet.
The corrected version
JavaScript (in a script tag)
const el = document.getElementById("message");
if (el) {
el.textContent = "Hello";
console.log(el.textContent);
}Browser console (the corrected version)
HelloHow to avoid it
Use defer on every script tag. Keep ids unique and in one place (constants) so a typo shows up once.
Questions
Why does the same code work when I paste it in the console?
By then the page is fully loaded, so the element exists.
This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.