JavaScript Lesson 02 — Connect a JavaScript File: script, defer and the Console
A JavaScript file does not run merely because it is in the project folder. HTML must reference it. Also, the element we want to change must exist when the code runs. These are two separate causes of a seemingly unresponsive page.
Check the connection
Keep index.html and app.js together, and include:
<script src="app.js" defer></script>
src is the file's path. Moving the script into a js subfolder requires src="js/app.js". A script element is not a void element, so include its closing tag even when loading an external file.
Write a diagnostic message
Keep the paragraph with id="message" from lesson one and use:
console.log('Dosya çalıştı.');
const message = document.querySelector('#message');
message.textContent = 'Bağlantı tamam.';
Open developer tools with F12 or the browser's developer-tools menu, then select Console. Refresh the page. The first line writes a diagnostic message there, not a paragraph into the visible page.
If no message appears, inspect the file path. The Network panel can show whether app.js was loaded. A failed local resource can also produce an error in the console.
Why use defer?
A classic external script in head without defer can execute before the browser has parsed the paragraph below it. Then querySelector returns null, and trying to set its textContent causes an error.
defer allows fetching the external file during parsing but postpones execution until parsing is complete. We are not using module scripts or async here. The opening examples consistently connect one external script this way.
Missing source or failing execution?
First inspect whether app.js loaded. Editing its statements cannot repair a missing path. If the file loaded, a console error may instead identify an expression that failed during parsing or execution.
A web server can accidentally return an HTML error page for a script URL. An unexpected less-than sign can be a clue. A .js filename does not prove the response contains JavaScript. In our local example, begin by checking the real adjacent file.
Defer is a timing rule for external classic scripts
It permits downloading while parsing continues, then executes after parsing. Moving a classic script into head without defer can make it run before the elements it needs exist.
Async is not an interchangeable spelling. It can execute when ready, before parsing finishes. Deferred classic scripts preserve document order; async execution can depend on download timing. Do not add async blindly as a speed improvement to dependent code.
Defer on an ordinary inline script does not provide the same external-script scheduling. Module scripts have their own behavior. We deliberately use one consistent connection method in the opening examples.
Parsing errors differ from execution errors
console.log('Before');
const amount = ;
console.log('After');
This intentionally invalid script cannot be parsed, so even its first log does not run. By contrast, a valid script referring to an undefined name can run up to that line. Checking whether the first message appeared helps distinguish the two.
Run deliberate failures separately or temporarily replace app.js. Appending invalid syntax to working code can prevent that whole script from executing. Restore the correct file afterwards.
Repeated logs can mean repeated loading
Check for duplicate script elements when a message appears twice. Repeated top-level declarations can also fail. Inspect the source, requests and first actual error rather than attributing every surprise to caching.
Save the file and refresh the correct tab. A later error can be a consequence of the first one, so investigate them in execution order.
Make a deliberate spelling mistake
console.log('Başlangıç');
console.log(mesage);
console.log('Bitiş');
This example is intentionally incorrect. No variable named mesage has been declared. The console reports a ReferenceError with a file and line reference. The first log runs; execution of this script stops at the error before reaching the last log.
Do not memorize the entire message. Follow its file and line reference, then find the undefined name. Restore the working script afterwards so that the next lesson begins without a leftover intentional failure.
References: Reference 1 · Reference 2