innerHTML vs textContent
Concept Check Medium
Question
Why is textContent safer than innerHTML for displaying user input?
Hint
Think about XSS — cross-site scripting.
Explanation
innerHTML parses the string as HTML, so user input like '<script>alert(1)</script>' would actually execute. textContent treats the string as plain text — the tags are displayed literally, not parsed. This prevents XSS attacks. Always use textContent (or textContent with escaping) for user-generated content.
Related Resources
Related Lessons
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.