execution order - Javascript variable declaration in head vs body -


is there difference between declaring variable in head , using in body so....

       <head>           <script>              var girlfriendname = "jennifer lawrence";            </script>        </head>         <body>             <script>              console.log("i wish girlfriend " + girlfriendname);            </script>         </body> 

and declaring , using in body so..

          <body>            <script>              var girlfriendname = "jennifer lawrence";            </script>             <script>              console.log("i wish girlfriend " + girlfriendname);            </script>          </body> 

is there ever case when might want declare them in head opposed body?

from functional point of view there no difference. recommended put js in (at end of) body.

this practise because it's more userfriendly. first entire dom , css loaded, result in page being displayed possible. if loading javascript takes lot of time, actual displaying might postponed or blocked, , page might change in shocking fashion. javascript didn't influence layout of page, doesn't matter if loaded bit later.


Comments