How to check if element exists in the html page using javascript?

Suppose it is a div with id “slots” then in jquery:

if( $(“#slots”).length < 1 ) {

console.log(“Div is not present”);
}else {

console.log(“Div with id #slots is present”);

}

its is more if we write any div dynamically.