How to find if element with specific id exists or not

You can simply use if(yourElement)

var a = document.getElementById("elemA");
var b = document.getElementById("elemB");

if(a)
  console.log("elemA exists");
else
  console.log("elemA does not exist");
  
if(b)
  console.log("elemB exists");
else
  console.log("elemB does not exist");
<div id="elemA"></div>

 var myEle = document.getElementById("myElement");
    if(myEle){
        var myEleValue= myEle.value;
    }

the return of getElementById is null if an element is not actually present inside the dom, so your if statement will fail, because null is considered a false value

Tags:

Javascript

Dom