JavaScript Security Best Practices Interview Questions and Answers

Review common web security risks and safe JavaScript practices for handling data and browser input.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

XSS is a type of attack where someone injects malicious JavaScript into a web page that other users visit. A classic example: if a site shows user-submitted comments without sanitizing the input, an attacker posts a comment with a <script> tag inside. When another user loads the page, that script runs in their browser and can steal cookies, hijack sessions, or do other damage.

The main defenses against XSS are:

  • Never put user input directly into HTML. Use textContent instead of innerHTML when displaying user-provided text
  • Escape special HTML characters (<, >, ", &) before displaying them
  • Use a Content Security Policy (CSP) header to restrict which scripts are allowed to run
  • Use frontend libraries (like DOMPurify) to sanitize HTML if you truly need to render user-provided HTML
  • Validate and sanitize all user input on the server side as well

javascript

// Vulnerable - never do this
element.innerHTML = userInput;

// Safe - use textContent
element.textContent = userInput;

CSRF (Cross-Site Request Forgery) is when a malicious site tricks a logged-in user into making a request to a site they are already authenticated with. For example, a hidden form on a bad site could silently submit to your bank's "transfer money" endpoint using your existing session cookie. The browser sends the cookie automatically. The key difference from XSS: XSS injects code that runs on the victim's page. CSRF makes the victim's browser fire a request to another site using their real credentials.

CSP is an HTTP response header that tells the browser which sources of scripts, styles, and other resources are trusted. It is one of the most effective defenses against XSS. For example, you can tell the browser to only run scripts from your own domain and block everything else, including any injected inline scripts.

http

Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-cdn.com

The same-origin policy is a browser security rule. It says that scripts from one origin (protocol + domain + port) cannot read data from a different origin. For example, a script on https://example.com cannot read the response of a fetch request made to https://other.com. The request might still be sent, but the browser blocks the response from being read. CORS (Cross-Origin Resource Sharing) is the mechanism that allows servers to explicitly say "I allow requests from this other origin."

Clickjacking is when a malicious page loads your site inside a transparent <iframe> and overlays it with a fake UI. The user thinks they are clicking a harmless button (like "Claim reward") but they are actually clicking something on your site underneath, like "Confirm payment" or "Delete account". You prevent this using the X-Frame-Options: DENY HTTP header or the frame-ancestors 'none' CSP directive, both of which tell the browser not to allow your page to be embedded in iframes.

http

X-Frame-Options: DENY
// or via CSP:
Content-Security-Policy: frame-ancestors 'none'

Use textContent or another context-appropriate safe text API rather than interpreting the value as HTML. Validate and encode according to the output context.

Code and bundled configuration delivered to a browser are visible to users. Keep private credentials on a trusted server and expose only appropriately scoped public values.

Updates can fix known vulnerabilities and bugs. Review advisories and changes, then test upgrades rather than blindly trusting every dependency.

Grant each account or token only the permissions needed for its task, and limit scope and lifetime to reduce impact if it is misused.

Exclude or redact secrets and personal data before logging, restrict log access, and define retention appropriate to the data and operational need.

Users can bypass browser checks and send requests directly. Enforce validation and authorization on the trusted server as well.

HTML text, attributes, URLs, CSS, and JavaScript have different parsing rules. Use APIs and encoders designed for the specific context instead of a single generic escaping step.

Use appropriate SameSite cookie settings and anti-CSRF tokens or other verified request-origin defenses for state-changing requests; do not rely on obscurity.

Hiding a UI control does not prevent direct requests. Enforce user and resource permissions at the trusted service boundary for each operation.