π§ MODULE 1: Introduction to JavaScript & Setup
Understand what JavaScript is, where it runs, how to connect it to your HTML pages, and how to use the browser console for debugging and experimentation.
1) What Is JavaScript?
JavaScript (JS) is the behavior layer of the web. It adds interactivity, logic, and dynamic changes to HTML and CSS.
| Layer | Language | Purpose |
|---|---|---|
| Structure | HTML | Defines content |
| Style | CSS | Defines appearance |
| Behavior | JavaScript | Defines interaction |
- Toggle menus, modals, and tabs
- Validate contact forms
- Animate or change elements dynamically
- Fetch live data (APIs)
- Save preferences (e.g., dark mode, language)
2) Setting Up Your Environment
Use the same portfolio project folder from your HTML & CSS course.
portfolio-site/
βββ index.html
βββ about.html
βββ projects.html
βββ contact.html
βββ css/
β βββ style.css
βββ js/
βββ app.js/js folder yet, create one.3) Linking JavaScript to Your Webpage
Open index.html and link your JavaScript file before the closing </body> tag:
<script src="js/app.js" defer></script>
</body>
</html>defer?- Loads the HTML first, then runs JS.
- Prevents βundefinedβ errors when elements arenβt ready.
defer for external scripts.4) Writing Your First JavaScript
In js/app.js, type this:
// app.js
console.log("Hello, StreetGeek!");
document.title = "StreetGeek Portfolio | JavaScript Active";Open your page β press F12 β Console tab β youβll see your message logged.
5) The Role of the Console
The browser console is your developer command line.
console.log("I love JavaScript!");
console.warn("This is a warning!");
console.error("Oops, an error happened!");6) The Anatomy of a Script
Basic building blocks:
// Single-line comment
/* Multi-line
comment */
let user = "Yogi";
console.log(`Welcome, ${user}!`);- Statements: Each line that performs an action.
- Comments: Notes ignored by the computer.
- Case-sensitive:
Userβuser. - Semicolons: Optionalβbe consistent.
7) How the Browser Executes JavaScript
- Browser reads the HTML.
- Encounters your
<script>tag. - Loads the JS file (and defers execution until HTML is parsed).
- Runs JS line-by-line, top to bottom.
8) Hands-On Practice
Exercise 1: Add a Welcome Message
// app.js
let name = "Yogi";
console.log(`Welcome to StreetGeek, ${name}!`);Now, change the text content of an element dynamically.
<h1 id="hero-title">Welcome!</h1>const heroTitle = document.getElementById("hero-title");
heroTitle.textContent = "Welcome to My Interactive Portfolio π";Exercise 2: Display Current Year in Footer
<footer>
<p>© <span id="year"></span> StreetGeek Academy</p>
</footer>const yearSpan = document.getElementById("year");
yearSpan.textContent = new Date().getFullYear();9) Quiz Time
Show/Hide Questions
1) What attribute should you use on your <script> tag to ensure it runs after the HTML loads?
2) Which console method displays errors?
3) Whatβs the difference between HTML, CSS, and JavaScript in a website?
4) Why is document.getElementById() useful?
5) What will this output in the console?
let city = "Richmond";
console.log(`I live in ${city}.`);π Challenge Task: Dynamic Greeting
<p id="greeting"></p>const greeting = document.getElementById("greeting");
const hour = new Date().getHours();
if (hour < 12) {
greeting.textContent = "Good morning βοΈ, welcome to StreetGeek!";
} else if (hour < 18) {
greeting.textContent = "Good afternoon π€οΈ, keep coding!";
} else {
greeting.textContent = "Good evening π, time to build something great!";
}Refresh β your greeting changes automatically based on the time of day.
π Module 1 Summary
- What JavaScript is and how it runs
- How to connect JS files with HTML
- How to use the browser console
- How to manipulate content dynamically
- How to write your first simple script
Next up β MODULE 2: Variables, Data Types & Operators