JavaScript Lesson 01 — What Is JavaScript? Why Do We Need It Alongside HTML and CSS?
HTML supplies the heading, paragraph and button. CSS can control their colors and layout. But what if clicking a button should mark an item as completed? JavaScript lets us add that behavior to the page.
JavaScript also runs outside browsers, for example through Node.js on a server. This course begins in the browser. You do not need Node.js, a package manager or a framework for the opening lessons.
Observe a change first
Put this content inside the body of index.html:
<h1>JavaScript denemesi</h1>
<p id="message">Bu metni HTML ile yazdım.</p>
<script src="app.js" defer></script>
Create app.js beside it and add:
const message = document.querySelector('#message');
message.textContent = 'JavaScript bu metni değiştirdi.';
The page displays the JavaScript sentence instead of the original HTML sentence. We did not delete the sentence from the file. The browser parsed the HTML, then JavaScript found the resulting paragraph and changed its text.
Read the two instructions
The first line finds the element with id="message" and keeps a reference named message. The hash in #message means an ID selector. The second line assigns new text to that element's textContent property. We will examine const and assignment in later lessons; for now, connect each line with its visible effect.
The script element connects the external file. defer makes that external script run after HTML parsing. The next lesson explains the timing more carefully.
Compare with JavaScript absent
Temporarily remove the script element, save and refresh. The original HTML sentence returns. Restore it and the replacement sentence appears. This gives us a simple comparison with and without the script.
“View page source” still shows the original sentence, whereas the Elements panel in developer tools shows the changed text. This is one reason the source file and the browser's current document can differ.
The first example contains two separate operations
Finding a paragraph and changing its text happen on different lines. Read this small program as sequential instructions. The second line depends on the result produced by the first.
Document is the browser's object for the current page. QuerySelector performs a selection. The quoted #message is a string supplying an ID selector. We keep the result under a variable name. That variable name does not have to match the HTML ID; the similarity merely helps readability.
On the next line, the dot accesses a property of that element. TextContent holds its text. The right side supplies a new value and the left side identifies where to assign it. Selection, naming, property access and assignment are all present in just two lines.
Break the relationship deliberately
Change the HTML ID to greeting without changing the selector. The selection returns null instead of an element, and the next attempt to set text fails. Seeing the original sentence therefore does not prove that the script failed to load; it may have loaded and then failed.
Restore the ID and refresh. This experiment shows that scripts depend on the HTML structure as well as correct filenames.
Did the disk file change?
No. The script changed the document living in that browser tab. The editor still contains the original sentence. Reloading reads the source again, then runs the script again. A repeated visible change is not evidence of a rewritten disk file.
Remove the script reference and refresh to confirm the original text returns. One visitor's local change also does not automatically change another visitor's page. Shared data and persistent storage require additional mechanisms.
The language and the browser environment
Variables, conditions and functions belong to the language. Document and element-selection operations are supplied by the browser environment. Node.js does not provide document by default. Staying in one environment helps us separate those concepts.
Rename the variable consistently on both lines without changing the selector. The example still works, demonstrating that a JavaScript variable name and an HTML ID are independent names.
Is this Java?
No. Java and JavaScript are separate languages. You do not need a Java installation to run this .js file. The browser's JavaScript engine executes it.
Change only the replacement sentence in the second line. If the visible page changes without editing the HTML file, you have identified the different jobs of the two files. Next we will connect scripts reliably and inspect errors that do not appear as page text.
References: MDN