What is the best practices of JavaScript to Initialise the variables

Viewed 23

I am confused about how to initialize the variables in best way possible in JavaScript. Should I write it as above the script as normal?

    let BodyEle = document.querySelector("body");
    let code = false;
    let Alert = document.querySelector(".alert");
    let element = document.getElementById("content");
    let menuBtn = document.querySelector(".menu");

   

Or Should I use the event listener to let load the Dom first

    window.addEventListener('DOMContentLoaded', function(event){

        // variables here
    
    });
0 Answers
Related