This lesson writes your very first JavaScript, straight in the browser. You'll learn how to embed code with the script tag, trigger a blocking alert() popup, print silent messages with console.log(), understand strings, and link an external JS file with the src attribute.
1First Steps in JavaScript Part 1
Welcome to your First Steps in JavaScript. Today, you'll stop being a spectator and start being an engineer. We're going to write our very first instructions.
// First Steps: Writing your first code2First Steps in JavaScript Part 2
To write JS inside an HTML file, we use the <script> tag. The browser executes anything inside these tags as code, not text.
<script>
// Code goes here
</script>3First Steps in JavaScript Part 3
Our first command is alert(). This 'Statement' tells the browser to display a popup. Note the parentheses and the semicolon at the end.
<script>
alert('Hello!');
</script>4First Steps in JavaScript Part 4
The text inside alert('...') is called a 'String'. Strings must always be wrapped in single or double quotes.
alert('This is a String');
alert("So is this");5First Steps in JavaScript Part 5
Popups are annoying. For debugging, we use console.log(). This prints messages to the 'Developer Tools', a hidden area just for programmers.
console.log('System initialized.');6First Steps in JavaScript Part 6
JS executes 'Sequentially'. This means it runs the first line, then the second, then the third. Top-to-bottom order is absolute.
console.log('1');
console.log('2');
console.log('3');7First Steps in JavaScript Part 7
Watch the render. See how 'alert' stops the page execution until you click 'OK', while 'console.log' runs silently in the background.
8First Steps in JavaScript Part 8
To run an external file, we use the 'src' attribute. This is the best way to organize your code as your app grows.
<script src='app.js'></script>9First Steps in JavaScript Part 9
You've successfully taken your first steps. You can now communicate with the browser and output data like a pro developer.
console.log('Level 1 Complete');10First Steps in JavaScript Part 10
First steps complete! Now let's master the rules of the language in Basic Syntax.
11Step-by-Step Breakdown
Welcome to your First Steps in JavaScript. Today, you'll stop being a spectator and start being an engineer. We're going to write our very first instructions.
To write JS inside an HTML file, we use the <script> tag. The browser executes anything inside these tags as code, not text.
Our first command is alert(). This 'Statement' tells the browser to display a popup. Note the parentheses and the semicolon at the end.
Checkpoint: Which HTML tag is used to embed JavaScript code?
- ā<style>
- ā<script>
The text inside alert('...') is called a 'String'. Strings must always be wrapped in single or double quotes.
Popups are annoying. For debugging, we use console.log(). This prints messages to the 'Developer Tools', a hidden area just for programmers.
JS executes 'Sequentially'. This means it runs the first line, then the second, then the third. Top-to-bottom order is absolute.
Watch the render. See how 'alert' stops the page execution until you click 'OK', while 'console.log' runs silently in the background.
Checkpoint: Where do 'console.log()' messages appear?
- āDirectly on the webpage
- āIn the Developer Console
To run an external file, we use the 'src' attribute. This is the best way to organize your code as your app grows.
You've successfully taken your first steps. You can now communicate with the browser and output data like a pro developer.
Checkpoint: What is the text data wrapped in quotes called in JavaScript?
- āBoolean
- āString
First steps complete! Now let's master the rules of the language in Basic Syntax.
Level Up š
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1alert() Dialogs Are Disruptive and Poorly Announced by Some Screen Readers
A native alert() blocks the entire page and can be jarring for assistive technology users, since it interrupts whatever the screen reader was reading. Prefer accessible, dismissible custom dialogs with proper ARIA roles (`role="alertdialog"`) for anything users will encounter regularly.
SEO Implications
- 1
Inline vs. External Scripts Affect Page Load and Rendering Speed
A large inline `<script>` block in the `<head>` can block HTML parsing until it finishes executing, delaying when content becomes visible. Loading JavaScript via an external file with the `defer` or `async` attribute lets the browser parse the rest of the page while the script downloads, which can improve perceived load speed and Core Web Vitals.
Best Practices
Prefer console.log Over alert() for Debugging
alert() halts all JavaScript execution and blocks user interaction with the page until dismissed, which makes it disruptive for both development and any user who encounters it. console.log() prints the same information to the Developer Tools without interrupting anything.
Load JavaScript with an External File and the src Attribute
Writing all your code inline inside `<script>` tags scattered through the HTML makes it hard to reuse, cache, or maintain. Linking a `.js` file with `src` lets the browser cache it separately from the HTML and keeps logic out of your markup.
Frequent Bugs
`ReferenceError: X is not defined` when logging a plain word instead of a string.
Writing `console.log(Hello World)` instead of `console.log('Hello World')` makes JavaScript try to interpret `Hello` as a variable name rather than text. Since no such variable exists, it throws a ReferenceError ā wrapping the text in quotes turns it into a string literal instead.
Real-World Examples
Linking an External Script and Logging a Startup Message
A new project needed to confirm that its JavaScript file was correctly linked to the HTML page before building out real features, so a simple console.log() was used as a sanity check.
<!-- index.html -->
<script src="app.js"></script>
// app.js
console.log('App initialized successfully.');