By the end of this lesson, learners will:
JavaScript was designed for the web, and every modern browser (Chrome, Firefox, Edge, Safari) includes a JavaScript engine that allows you to run your code without installing anything else.
π§ Key Insight: Your browser is your JavaScript playground!
The console is a tool inside the browser that lets you:
console.log("Hello, Codevisionz!");
π‘ This is a great way to test small bits of code and practice syntax without needing an HTML file.
Letβs write a basic web page with JavaScript embedded directly inside.
<!DOCTYPE html>
<html>
<head>
<title>My First JS Page</title>
</head>
<body>
<h1>Hello from HTML</h1>
<script>
alert("Hello from JavaScript!");
</script>
</body>
</html>
alert()
function when the page loads, showing a popupπ Save this as index.html
and double-click it to open in your browser.
As your code grows, it’s best to separate your JavaScript from your HTML.
HTML File (index.html):
<!DOCTYPE html>
<html>
<head>
<title>External JS Example</title>
</head>
<body>
<h1>External JS Demo</h1>
<script src="script.js"></script>
</body>
</html>
JavaScript File (script.js):
console.log("JavaScript is running from an external file!");
π Save both files in the same folder, then open index.html
.
β This keeps your code clean and modular.
Practice | Explanation |
---|---|
β Use external JS | Keeps code organized and easier to maintain |
β
Place scripts at bottom of <body> | Ensures the HTML loads before the script runs |
β Avoid inline JavaScript | Harder to read, maintain, and debug |
β
Use console.log() to test code | Safer and cleaner than using alerts everywhere |
Function | What It Does |
---|---|
console.log() | Logs messages to the console |
alert() | Shows a popup box |
document.write() | Writes directly into the page (not recommended for modern use) |
Try this basic interaction:
<!DOCTYPE html>
<html>
<head>
<title>Click Event Demo</title>
</head>
<body>
<button onclick="alert('You clicked me!')">Click Me</button>
</body>
</html>
β Save and open in your browser. Clicking the button runs JavaScript!
Concept | Explanation |
---|---|
Browser Console | Lets you test and debug JS code instantly |
Internal Script | JavaScript code inside a <script> tag in HTML |
External Script | JavaScript stored in a separate .js file |
alert() | Pops up a dialog box |
console.log() | Outputs text to the developer console |