Step: Basics | Players: None (beginners in programming welcome)
Learning Objectives
- Understand in what environment JavaScript runs (browser, Node.js).
- You can output the value with
console.logand check the result. - You can connect JS to HTML using the
<script>tag. - You can use the Console tab of the browser developer tools (DevTools).
- Can explain the meaning and effects of
"use strict"mode.
1. JavaScript execution environment
JavaScript originally started out as a language that ran in browsers, but now, thanks to Node.js, it's used almost everywhere, including servers, CLIs, and build tools. Even with the same code, the available APIs are different depending on the execution environment (e.g. window for browser, process for Node).
console.log("Hello, JavaScript!");
The above line works the same in both the browser console and Node node file.js.
2. Script connection and execution order
To run JS in HTML, use the <script> tag. If you attach the defer attribute, it is executed after HTML parsing is completed, making DOM access safe.
<script src="src/01_console.js" defer></script>
In the Node environment, run node src/01_console.js in the terminal.
3. Developer tools and debugging basics
Press F12 (or Cmd+Option+I) in your browser to open DevTools. You can immediately evaluate JS expressions in the Console tab, and set breakpoints in your code in the Sources tab. Debugging becomes much easier if you use console.log, console.table, and console.error according to the situation.
4. Strict mode
If you write "use strict"; at the top of the file, common mistakes (assigning a value to an undeclared variable, etc.) will be reported as errors. The ES module (type="module") is automatically strict.
"use strict";
x = 10; // ReferenceError: x is not defined
Core example
| file | Contents covered |
|---|---|
01_console.js |
Comparison of output of various console methods |
02_script_tag.js |
Loaded from HTML and output message to DOM |
03_devtools.js |
Debugging patterns used in DevTools |
04_strict_mode.js |
Check the effect of strict mode |
⚠️ Common Mistakes
- When writing
lofconsole.login capital letters (console.Logis an error). - If you just put
<script>in<head>and want to access it when the DOM does not exist yet → usedefer. - When ASI (Automatic Semicolon Insertion) operates incorrectly because a line is continued without a semicolon.
- Call browser-specific APIs such as
alertanddocumentfrom Node executable file. - If you refresh your browser without saving the file and realize that the changes are not reflected.
❓ Frequently Asked Questions (FAQ)
Q1. Which is better to practice on: Node or the browser? A. While only handling input and output, node is fast and convenient. From the DOM section onwards, a browser is required.
Q2. Besides console.log, which one do you often use? A. console.table (array/object table), console.error (red output), and console.time/timeEnd (performance measurement) are useful.
Q3. Do I have to use strict mode? A. New code is automatically strict because it is usually written as an ES module. Writing it explicitly makes your intentions clear.