title: Why this code gives me "Uncaught TypeError: Cannot read properties of null (reading 'push')" error?
body: <p>In this code I am getting error as Uncaught TypeError: Cannot read properties of null (reading 'push')</p>
  <pre><code>console.log(&quot;HI&quot;);
  let addbtn = document.getElementById(&quot;addbtn&quot;);
  addbtn.addEventListener(&quot;click&quot;, function (e) {
    let addtxt = document.getElementById(&quot;addtxt&quot;);
    let notes = localStorage.getItem(&quot;notes&quot;);
    if (notes == null) {
      notesObj = [];
    } else {
      notesObj = JSON.parse(notes);
    }
  
    notesObj.push(addtxt.value);
    //getting error at this point. Don't know why
    localStorage.setItem(&quot;notes&quot;, JSON.stringify(notesObj));
    addtxt = &quot;&quot;;
    console.log(notesObj);
  });
  </code></pre>