JavaScript Tutorial
Work with values, transform data and add a browser interaction.
本课程正文目前为英文。
Before you begin
Use a modern browser console for the first three lessons. The final lesson needs an HTML document; DOM APIs are browser APIs, not part of the JavaScript language itself.
Follow the four chapters in order, then complete the practice task. These are introductory lessons, not an exhaustive language reference.
CHAPTER 1 / 4
Values and variables
Use const for a binding you do not reassign and let for a binding you change. Numbers and strings behave differently with +. Convert external text deliberately before doing arithmetic. Run each example in a fresh console scope to avoid redeclaring const.
const price = 12;
const quantity = Number("3");
console.log(price * quantity);
console.log("12" + 3);Expected result
The console prints 36, then the string 123.
Common mistake
const does not freeze an object. Number("") is 0, so check empty input separately when that distinction matters.
Try it yourself
Predict Number("three") and test whether it is a valid number.
Check your answer
It is NaN. Number.isNaN(Number("three")) returns true.
CHAPTER 2 / 4
Functions and decisions
A function names a reusable calculation. Return sends a result to the caller; logging only displays it. Strict equality compares without the coercion performed by loose equality.
function shipping(total) {
if (total >= 50) return 0;
return 5;
}
console.log(shipping(30));
console.log(shipping(50));
console.log(3 === "3");Expected result
The output is 5, 0 and false. The boundary value 50 receives free shipping.
Common mistake
A missing return produces undefined. Test boundary values rather than only a typical input.
Try it yourself
Change the free-shipping threshold to 75 and test 74, 75 and 76.
Check your answer
The shipping results should be 5, 0 and 0.
CHAPTER 3 / 4
Transform a list of objects
An array stores an ordered sequence. Filter selects entries; map transforms each entry into a new value. These methods return new arrays, but they do not deeply clone objects stored inside them.
const projects = [
{ name: "Garden", done: true },
{ name: "Notebook", done: false }
];
const names = projects.filter(project => !project.done).map(project => project.name);
console.log(names.join(", "));Expected result
The console prints Notebook. The original projects array still has two entries.
Common mistake
An arrow function with braces needs an explicit return to return a value. Array indexes start at zero.
Try it yourself
Add an unfinished project called Weather and run the transformation again.
Check your answer
The printed string becomes Notebook, Weather.
CHAPTER 4 / 4
Practice: an interactive counter
Save this complete example as counter.html and open it in a browser. The script follows the markup so the elements already exist. A click handler updates state and then writes text to the output element.
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Project counter</title>
<button type="button" id="add">Add project</button>
<output id="count" aria-live="polite">0</output>
<script>
let count = 0;
const button = document.querySelector("#add");
const output = document.querySelector("#count");
button.addEventListener("click", () => {
count += 1;
output.textContent = String(count);
});
</script>
</html>Expected result
Each click increases the visible number by one. Reloading resets it to zero.
Common mistake
querySelector can return null when the selector does not match. Use textContent for text rather than injecting untrusted strings with innerHTML.
Try it yourself
Add a Reset button and make it set the state and displayed count to zero.
Check your answer
Listen for its click, assign count = 0, then assign output.textContent = String(count). Test add, reset, then add again.
Continue learning
Review the examples against the documentation, then change one input or layout constraint and explain the result.