How scripts, DOM, and variables work together
JavaScript is written inside the <script> tag.
It is usually placed at the end of the <body>.
<script> // JavaScript code here </script>
JavaScript uses variables to store data.
const = value does NOT changelet = value can changeconst name = "Anna"; let count = 0; count = count + 1;
document represents the whole HTML page.
It allows JavaScript to access elements.
document.getElementById("output");
An element is a part of the HTML page (like a div, input, or paragraph).
const el = document.getElementById("demo");
el.textContent = "Hello!";
JavaScript reads HTML elements, changes them, and updates the page.
<input id="name">
<button onclick="show()">Click</button>
<p id="output"></p>
<script>
function show() {
const value = document.getElementById("name").value;
document.getElementById("output").textContent = value;
}
</script>
Events are actions like clicks or typing.
button.onclick = function() {
alert("Clicked!");
};