Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 9 of 10 · DOM Manipulation and Events
Interview question

What is the difference between innerHTML, innerText, and textContent? innerHTML, innerText, और textContent में क्या अंतर है?

Answer
PropertyParses HTML?Respects CSS visibility?Performance
innerHTMLYes - renders tags as elementsN/A (sets raw markup)Slower, triggers reflow/reparse
innerTextNo - plain text onlyYes - ignores hidden elements, aware of CSSSlower - triggers layout/reflow to compute visible text
textContentNo - plain text onlyNo - includes hidden text tooFastest - no layout calculation needed
// HTML: <div id='box'>Hello <span style='display:none'>Hidden</span> World</div>

const box = document.getElementById('box');

console.log(box.innerHTML);    // 'Hello <span style="display:none">Hidden</span> World'
console.log(box.innerText);    // 'Hello World' - skips hidden text
console.log(box.textContent);  // 'Hello Hidden World' - includes hidden text

// Setting values
box.innerHTML = '<strong>Bold</strong>'; // renders as bold text
box.textContent = '<strong>Bold</strong>'; // renders literally as the string '<strong>Bold</strong>'

// XSS risk with innerHTML and user input
const userInput = '<img src=x onerror="alert(1)">';
box.innerHTML = userInput; // DANGEROUS - executes the script
box.textContent = userInput; // SAFE - displays as plain text

// Performance tip: use textContent when you don't need to parse HTML
// and don't need to account for visibility (most common case for plain text)
PropertyHTML parse करता है?CSS visibility respect?Performance
innerHTMLहाँN/Aधीमा
innerTextनहींहाँ - hidden elements ignoreधीमा
textContentनहींनहींसबसे तेज़
const box = document.getElementById('box');

console.log(box.innerHTML);    // HTML tags सहित
console.log(box.innerText);    // hidden text छोड़ देता है
console.log(box.textContent);  // hidden text सहित सब कुछ

box.innerHTML = '<strong>Bold</strong>'; // bold render होगा
box.textContent = '<strong>Bold</strong>'; // literal string दिखेगा

// innerHTML से XSS risk
const userInput = '<img src=x onerror="alert(1)">';
box.innerHTML = userInput;   // खतरनाक
box.textContent = userInput; // सुरक्षित

Was this answer clear?