JavaScript includes 方法
✏️ Editable
Loading...
Live Preview
⌘+Enter Run⌘+R Reset
About This Exercise
你可以把 includes 理解成一句自然语言:“里面有没有这个值?”
这一关会让你在三个常见业务里反复练这个动作:
- 标签筛选(数组)
- 标题搜索(字符串)
- 批量过滤(数组 + filter)
写完后你会发现:很多判断题其实一行就能写清楚。
Beginner⏱ 12-18 min
Learning Objectives
- 看懂并写出 Array.includes 的基础判断
- 理解 String.includes 在关键词搜索里的作用
- 将 includes 和 filter 组合完成简单筛选
Scenario
你在做一个“课程搜索面板”:
用户输入关键词、勾选标签后,页面要快速告诉用户“是否命中”并展示结果。
Why Do This
- Frontend JS is not just about knowing the syntax — the key is reliably manipulating the DOM and managing interaction state.
- Break down the event flow (trigger -> handle -> update UI) before coding, and you will make significantly fewer errors.
- Rule-based validation helps you develop a testable frontend mindset.
Hands-on Practice
- Write out the event flow in comments first, then implement the functions.
- Add an edge case branch for invalid input or empty data.
- Explain why each validation rule is needed.
Common Mistakes
- Only testing the happy path without verifying edge inputs or repeated clicks.
- Mixing event binding and state updates together, making future maintenance difficult.
- Console output looks correct but the UI is not actually updated.
Deliverable
A reusable interaction component scaffold (event flow description + key functions + edge case handling).
My Progress
Completed Steps0 / 3
Total Attempts0
Best Score0%
Completion Criteria (Checkable)
Confirm before completing this Lab
Reflection Questions (Recommended)
Describe the event flow of this Lab in 2-3 sentences (trigger -> handle -> update).
What edge case did you add? Why is it easy to miss?
How confident are you with this interaction pattern now?
Tags
JavaScriptES7Array.includesString.includes