Posts

Showing posts with the label Why?

Why Semicolons are needed for every line of JavaScript?

Image
All statements in JavaScript must end with a semi-colon, to indicate that this is where the statement ends. If you don't include these, you can get unexpected results. This ends with the strange behaviour of your code.

Why to Keep the <script> tag always at bottom?

Image
The reason we've put the <script> element near the bottom of the HTML file is that HTML is loaded by the browser in the order it appears in the file. If the JavaScript is loaded first and it is supposed to affect the HTML below it, it might not work, as the JavaScript would be loaded before the HTML it is supposed to work on. Therefore, putting JavaScript near the bottom of the HTML page is often the best strategy.