Introduction to JavaScript
If HTML is the skeleton and CSS is the skin, then JavaScript (JS) is the brain.
JavaScript is what makes a website do things. It handles what happens when you click a button, how data is saved, and how information updates without refreshing the whole page.
1. The Three Layers of a Web Page
To be a Full-Stack developer, you must understand how these three work together:
- HTML (Structure): "There is a button here."
- CSS (Style): "The button is blue and rounded."
- JavaScript (Logic): "When the button is clicked, show a 'Thank You' message."
2. Where Does JavaScript Live?
Like CSS, you can add JavaScript to your project in different ways. At CodeHarborHub, we always recommend the External method to keep your "brain" separate from your "body."
- External (Recommended)
- Internal
Create a file named script.js.
In your HTML (at the bottom of <body>): script src="script.js"></script>
Write code inside <script> tags.
In your HTML: <script> alert('Hello!'); </script>
3. Your First Commands
JavaScript is a language of instructions. Here are the three most common ways to see if your "brain" is working:
console.log(): Prints a message in the browser's hidden "Developer Console." This is for developers to test things.alert(): Pops up a message box for the user.prompt(): Asks the user to type something in.
console.log("Welcome to CodeHarborHub!");
alert("Warning: Learning JS is addictive!");
4. The Developer Console
Every browser has a secret tool for developers.
- Right-click any website.
- Click Inspect.
- Click the Console tab.
You can actually type JavaScript directly into this console and it will run instantly!
Let's Try It!
- Open your project folder.
- Create a file named
index.htmland a file namedscript.js. - Link them in your HTML:
index.html
<body>
<h1>My JS Test</h1>
<script src="script.js"></script>
</body> - In
script.js, type:script.jsalert("JavaScript is connected!");
console.log("I am a future Full-Stack Developer."); - Open your HTML in the browser. If the popup appears, you've just written your first script!
JavaScript is the most popular programming language in the world. Once you learn it for the web, you can use it to build mobile apps, desktop software, and even control robots!
Always put your <script> tag at the very bottom of your <body>. This ensures your HTML (the skeleton) loads before the JavaScript (the brain) starts trying to control it.