JavaScript 字符串方法
✏️ Editable
Loading...
Live Preview
⌘+Enter Run⌘+R Reset
About This Exercise
前端每天都在处理字符串:用户名、标题、搜索词、邮箱输入。
这关只练最常用的三类能力:
你将学到:
- 格式化:首字母大写,其余小写
- 拼接:模板字符串输出完整文案
- 校验:判断是否包含特定符号
Beginner⏱ 15-20 min
Learning Objectives
- 掌握 toUpperCase/toLowerCase/slice 的组合用法
- 掌握模板字符串插值语法
- 掌握 includes 的基础校验写法
- 理解字符串方法返回新值而非修改原值
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
JavaScriptStringsMethodsTemplate Literals