Master JavaScript fundamentals, DOM manipulation, events, and interactive web application development. 掌握JavaScript基礎知識、DOM操作、事件處理,以及互動式網頁應用程式開發。
Navigate to any section of this interactive study guide. 點擊任意卡片跳轉至對應學習章節。
Understanding the role of JavaScript in modern web development. 了解JavaScript在現代網頁開發中的角色。
Code (JavaScript) that runs in the user's browser, not on the server. It enables dynamic, interactive web pages without server round-trips. 在用戶瀏覽器中執行的程式碼(JavaScript),而非在伺服器上執行。它使網頁無需向伺服器發送請求即可實現動態互動效果。
The primary client-side scripting language for the web. JavaScript adds interactivity to HTML pages and can manipulate page content dynamically. 網頁最主要的客戶端腳本語言。JavaScript為HTML頁面添加互動性,並能動態操控頁面內容。
An API that represents the HTML document as a tree of objects. JavaScript uses the DOM to read and modify page content, structure, and style. 將HTML文件表示為物件樹的API。JavaScript使用DOM來讀取和修改頁面內容、結構和樣式。
A programming paradigm where code execution is triggered by events (user clicks, key presses, page loads), making web apps interactive. 一種程式設計範式,程式碼的執行由事件(用戶點擊、按鍵、頁面載入)觸發,使網頁應用程式具有互動性。
Browser tools accessed with F12 key. Includes Elements tab (HTML/CSS), Console (JavaScript output/errors), Network (HTTP requests). 按F12鍵訪問的瀏覽器工具。包含Elements標籤(HTML/CSS)、Console(JavaScript輸出/錯誤)、Network(HTTP請求)。
Describe client-side scripting · Understand JS constructs & data types · Implement control structures · Manipulate DOM · Use events & handlers 描述客戶端腳本的用途 · 理解JS構造及數據類型 · 實現控制結構 · 操控DOM · 使用事件與事件處理器
let
A. 變數 — 使用 let 宣告
In JavaScript, variables are declared using the let keyword. A variable stores a value that can be read and changed later.
在JavaScript中,變數使用 let 關鍵字宣告。變數儲存一個可以稍後讀取和修改的值。
| Data Type數據類型 | Example例子 | Notes備注 |
|---|---|---|
| Number | 0.0, 1.0, 2.5, -300.1 |
All numbers are floating-point (no separate Integer type)所有數值均為浮點數(沒有獨立的整數類型) |
| Boolean | true, false |
Must be lowercase; don't use quotes必須小寫;不要加引號 |
| String | "Hello World" |
A series of characters enclosed in double quotes用雙引號括起來的字符序列 |
Arithmetic Operators算術運算符
Shorthand Assignment簡寫賦值
String Concatenation (+): Combines strings together.
字串連接(+):將字串合併在一起。
Template Literal: Insert variables into strings using backticks and ${}.
模板字面量:使用反引號和 ${} 將變數插入字串中。
Relational Operators關係運算符
| Operator運算符 | Meaning含義 |
|---|---|
== | Equal等於 |
!= | Not equal不等於 |
< | Less than小於 |
> | Greater than大於 |
<= | Less than or equal小於或等於 |
>= | Greater than or equal大於或等於 |
Logical Operators邏輯運算符
| Operation運算 | Operator運算符 |
|---|---|
| AND | && |
| OR | || |
| NOT | ! |
if...elseif...else
if...else if...elseif...else if...else
while loopwhile 循環
for loopfor 循環
The id attribute uniquely identifies an HTML element within a page. It is used by JavaScript to select a specific element for manipulation.
id 屬性在頁面中唯一標識一個HTML元素。JavaScript用它來選取特定元素進行操作。
id must be unique within the same HTML document. Multiple elements can exist without an id, but if they have one, it must not be repeated.
每個 id 在同一HTML文件中必須唯一。元素可以沒有 id,但如果有,則不能重複。
document.getElementById("id") to select the HTML element with the specified id.
使用 document.getElementById("id") 選取指定id的HTML元素。
.innerText property to change the displayed text.
將新字串賦值給 .innerText 屬性以更改顯示的文字。
Changes only the plain text content. HTML tags in the string are treated as literal text, not parsed as HTML. 只更改純文字內容。字串中的HTML標籤被視為純文字,不會被解析為HTML。
Changes the HTML markup inside the element. HTML tags in the string ARE parsed and rendered by the browser. 更改元素內的HTML標記。字串中的HTML標籤會被瀏覽器解析和渲染。
innerText for plain text changes. Use innerHTML when you need HTML formatting (bold, links, etc.).
提示:純文字更改使用 innerText。需要HTML格式(粗體、連結等)時使用 innerHTML。
This example shows how to retrieve text from an element, convert it to a number, and write it back.
此範例展示如何從元素讀取文字、轉換為數字,再寫回元素。
innerText always returns a string. Use Number() to convert it to a number before doing arithmetic.
innerText 總是返回字串。在進行算術運算之前,需要使用 Number() 將其轉換為數字。
A function groups reusable code under a name. It is defined once and can be called (invoked) multiple times.
函數將可重用的程式碼歸納在一個名稱下。它只需定義一次,可以被多次調用(執行)。
| Event事件 | Element元素 | Description描述 |
|---|---|---|
click |
<button> |
User clicked the button用戶點擊了按鈕 |
change |
<input type="text"> |
Text in input field has changed輸入框中的文字已更改 |
copy |
<p> |
User copied text from the paragraph用戶從段落中複製了文字 |
focus |
<input type="password"> |
User focused on the password field用戶聚焦於密碼欄 |
submit |
<form> |
HTML form is being submittedHTML表單正在提交 |
An event handler is a function that runs when an event occurs. Register it by assigning the function to an event attribute (e.g., onclick).
事件處理器是事件發生時執行的函數。通過將函數賦值給事件屬性(如 onclick)來註冊它。
<head> section so they are available before the page body loads. The onclick attribute links the button's click event to the function.
函數應在 <head> 部分定義,以便在頁面主體載入之前可用。onclick 屬性將按鈕的點擊事件連結到函數。
The submit event on a form can be used to validate user input before sending data to the server. This prevents invalid data from being sent.
表單的 submit 事件可用於在將數據發送到伺服器之前驗證用戶輸入。這可以防止無效數據被發送。
15 questions covering all topics in Chapter 5. Select an answer and submit to see your score. 涵蓋第五章所有主題的15道題目。選擇答案後提交以查看你的得分。
let is the standard keyword for declaring variables. It allows reassignment but is block-scoped.
答案:B — let。在現代JavaScript中,let 是宣告變數的標準關鍵字。它允許重新賦值,但有塊作用域。
document.getElementById("name").innerText = "Alice" do?
2. document.getElementById("name").innerText = "Alice" 的作用是什麼?
document.getElementById() selects the element, and assigning to .innerText changes its displayed text content.
答案:B。document.getElementById() 選取元素,賦值給 .innerText 更改其顯示的文字內容。
let x = 5; x += 3; console.log(x);
3. 以下程式碼的輸出是什麼:let x = 5; x += 3; console.log(x);
x += 3 is shorthand for x = x + 3, so 5 + 3 = 8.
答案:C — 8。x += 3 是 x = x + 3 的簡寫,所以 5 + 3 = 8。
${} variable interpolation.
答案:C。模板字面量必須使用反引號(`)。單引號和雙引號不支持 ${} 變數插值。
innerText and innerHTML?
5. innerText 和 innerHTML 有什麼區別?
innerText treats the string as plain text (HTML tags shown literally). innerHTML parses HTML tags and renders them.
答案:B。innerText 將字串視為純文字(HTML標籤按字面顯示)。innerHTML 解析HTML標籤並渲染它們。
let a = [10, 20, 30]; console.log(a[1]); output?
6. let a = [10, 20, 30]; console.log(a[1]); 的輸出是什麼?
onclick attribute registers a function as the handler for the click event on an HTML element.
答案:B — onclick。onclick 屬性將函數註冊為HTML元素點擊事件的處理器。
true or false. Using quotes makes it a string, not a Boolean.
答案:C — true。JavaScript布林值必須小寫:true 或 false。使用引號會使其成為字串,而非布林值。
Number() function do?
9. Number() 函數的作用是什麼?
Number() converts a value (typically a string retrieved from innerText) to a numeric type so arithmetic can be performed.
答案:B。Number() 將一個值(通常是從 innerText 獲取的字串)轉換為數值類型,以便進行算術運算。
let x = 10; while (x < 30) { x += 10; } console.log(x);
10. 以下程式碼的輸出是什麼:let x = 10; while (x < 30) { x += 10; } console.log(x);
&& for AND, || for OR, and ! for NOT.
答案:C — &&。JavaScript使用 && 表示AND,|| 表示OR,! 表示NOT。
<head><script>...</script></head> so they are loaded and available before the page body renders.
答案:B。函數應放在 <head><script>...</script></head> 中,這樣在頁面主體渲染之前它們就已載入並可用。
members.push("Kelvin") do to an array?
13. members.push("Kelvin") 對陣列做了什麼?
push() method adds a new element to the END of an array and returns the new length.
答案:C。push() 方法將新元素添加到陣列的末尾,並返回新的長度。
10 % 3 in JavaScript?
15. JavaScript中 10 % 3 的結果是什麼?
% (modulus) operator returns the remainder of division. 10 ÷ 3 = 3 remainder 1.
答案:D — 1。%(餘數)運算符返回除法的餘數。10 ÷ 3 = 3 餘 1。
Click each card to flip and reveal the definition. 24 cards covering all Chapter 5 vocabulary. 點擊每張卡片翻轉以查看定義。24張卡片涵蓋第五章所有詞彙。
Practice questions modelled on DSE and school examination formats. Click "View Reference Answer" to see the model answer. 參考DSE及學校考試格式的練習題。點擊「查看參考答案」查看參考答案。
score and assigns it the value 100 in JavaScript?
以下哪個選項正確地在JavaScript中宣告了一個名為 score 的變數並賦值為100?
int score = 100;let score = 100;var score == 100;score := 100;
let score = 100; is the correct modern JavaScript syntax. int is a PHP/Java keyword, not used in JavaScript. == is a comparison operator, not assignment. := is not valid JavaScript syntax.
let score = 100; 是正確的現代JavaScript語法。int 是PHP/Java的關鍵字,JavaScript中不使用。== 是比較運算符,不是賦值運算符。:= 不是有效的JavaScript語法。
innerText and innerHTML when used to modify an HTML element's content.
說明在修改HTML元素內容時,innerText 和 innerHTML 之間的一個區別。
innerText sets the plain text content of an element — any HTML tags in the assigned string are displayed as literal text characters (1 mark). innerHTML sets the HTML markup content — HTML tags in the string are parsed and rendered by the browser, allowing formatting changes (1 mark).
innerText 設置元素的純文字內容——賦值字串中的HTML標籤以字面文字字符顯示(1分)。innerHTML 設置HTML標記內容——字串中的HTML標籤由瀏覽器解析和渲染,允許格式更改(1分)。
id attribute is used for in HTML and why it is important when using JavaScript to manipulate web pages.
解釋HTML中 id 屬性的用途,以及在使用JavaScript操控網頁時它為何重要。
id attribute is a unique identifier for an HTML element within a web page (1 mark). It is important because JavaScript uses document.getElementById() to select a specific element for manipulation — without a unique id, it would not be possible to target a specific element (1 mark).
id 屬性是網頁中HTML元素的唯一標識符(1分)。它很重要,因為JavaScript使用 document.getElementById() 來選取特定元素進行操作——沒有唯一的id,就無法針對特定元素(1分)。
<p id="score">) is 50 or above. If yes, display "Pass" in <p id="result">; otherwise display "Fail".
完成以下JavaScript函數。該函數應檢查學生的分數(儲存在 <p id="score"> 中)是否達到50分或以上。如果是,在 <p id="result"> 中顯示"Pass";否則顯示"Fail"。
>=), correct use of getElementById and innerText for output.
關鍵點:正確的比較運算符(>=),正確使用 getElementById 和 innerText 進行輸出。
countEven(arr) that takes an array of numbers and returns the count of even numbers in the array. (Hint: A number is even if number % 2 == 0)
編寫一個JavaScript函數 countEven(arr),接受一個數字陣列並返回陣列中偶數的個數。(提示:如果 number % 2 == 0,則該數字為偶數)
click event) (1 mark + 1 mark for example). An event handler is a JavaScript function that is registered to execute when a specific event occurs (1 mark). They work together by registering the function as a handler using an event attribute — e.g., onclick="myFunction()" on a button means myFunction() runs whenever the button is clicked (1 mark).
事件是在網頁應用程式中發生的動作,由用戶或瀏覽器觸發(例如,用戶點擊按鈕——click 事件)(1分 + 例子1分)。事件處理器是當特定事件發生時被註冊執行的JavaScript函數(1分)。它們通過使用事件屬性將函數註冊為處理器來協同工作——例如,按鈕上的 onclick="myFunction()" 表示每次點擊按鈕時 myFunction() 都會執行(1分)。
<input id="answer">, (b) compares it to the correct answer "JavaScript", (c) displays "Correct!" or "Wrong! The answer is JavaScript" in <p id="feedback">, and (d) includes a button that triggers this function.
Hollis正在開發一個測驗應用程式。編寫完整的JavaScript和HTML,實現以下功能:(a) 從 <input id="answer"> 讀取用戶答案,(b) 與正確答案"JavaScript"進行比較,(c) 在 <p id="feedback"> 中顯示"Correct!"或"Wrong! The answer is JavaScript",(d) 包含觸發此函數的按鈕。
.value (not .innerText) to read from <input> elements.
注意:讀取 <input> 元素時使用 .value(而非 .innerText)。
i = 1 instead of i = 0. Arrays are 0-indexed, so the first element (80) is skipped (2 marks).i <= scores.length instead of i < scores.length. When i = 3 (= length), scores[3] is undefined, causing incorrect total (2 marks).
錯誤1:循環從 i = 1 而不是 i = 0 開始。陣列從索引0開始,所以第一個元素(80)被跳過(2分)。i <= scores.length 而不是 i < scores.length。當 i = 3(= 長度)時,scores[3] 是 undefined,導致總和不正確(2分)。