JavaScript is a versatile and powerful programming language that runs in the browser (and on servers) to add interactivity and logic to web pages. With JavaScript, you can handle user actions, validate forms, animate elements, update content without reloading, and much more.
Think of HTML as the structure, CSS as the design, and JavaScript as the brain that makes everything respond and feel alive.
Node.js.JavaScript syntax is the set of rules that define how JavaScript programs are written and understood by the browser.
; (optional but recommended).{ }, for example in functions and loops.// single-line or /* multi-line */.
// Single-line comment
/*
Multi-line
comment
*/
let name = "John";
console.log(name);
Here, let declares a variable, and console.log() prints its value to the browser's developer console.
This simple example shows JavaScript displaying a popup message when the page loads:
<script>
alert("Welcome to JavaScript!");
</script>
There are three common ways to include JavaScript in an HTML page:
onclick).<script> tag in the HTML file..js file linked to your HTML.
<script>
console.log("Hello from internal script!");
</script>
<script src="script.js"></script>
JavaScript can produce output in different ways:
alert("Hello") – shows a popup alert box.document.write("Text") – writes directly into the HTML page (mostly for simple demos).console.log("Log") – prints a message to the browser's console (great for debugging).innerHTML – updates the content inside an HTML element.Example: using innerHTML to change the content of a paragraph:
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
Click the button below to update this text using JavaScript.
console.log() often to see what your code is doing..js files for cleaner HTML.defer so the page loads faster.getElementById, querySelector, etc."Hello, this is JavaScript!" when clicked.innerHTML.console.log().Example button with an inline JavaScript alert: