← Web & Frontend
#9 most askedVery commonMediumWeb security

Cross-site scripting (XSS)

🏢 Asked in real interviews
No reports yet. Were you asked this? Be the first to say where.
I was asked this

What is XSS, and how do you prevent it?

🎤 Answer it out loud, then check yourself

Say your answer as if the interviewer is listening (30–90 seconds). Then reveal what a strong answer covers and tick what you said.

    Try it · run and change it
    const comment = '<img src=x onerror="alert(document.cookie)">';
    const escape = (s) => s.replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" })[c]);
    console.log(escape(comment));
    
    Answer out loud first, then compare.
    Want to rehearse it?
    Answer this to the AI interviewer and get a verdict →