This is my code; If the ip variable is null, it does not return an error.
script;
function addIP(ip) {
var PRE = document.createElement('PRE');
PRE.textContent = ip;
document.getElementById('Leak').appendChild(PRE);
document.getElementById('p1').remove();}
html;
<td id="Leak"><pre id="p1">No Leak</pre></td>
error in console;
Uncaught TypeError: Cannot read property 'remove' of null
EDIT; solved in this way.
var node = document.getElementById ('p1');
if (node !== null) {document.getElementById('p1').remove();}