JS आउटपुट
In this page:
console.log(value);
document.getElementById("id").innerHTML = value;
document.write(value);
innerHTML का उपयोग
innerHTML आपको किसी element के भीतर की HTML सामग्री पढ़ने या बदलने देता है, जो इसे webpage पर सीधे गतिशील output दिखाने का सबसे आम तरीका बनाता है।
उदाहरण: Using innerHTML
document.body.innerHTML = "<h2>Hello!</h2><p>This text was added with innerHTML.</p>";
window.alert() का उपयोग
alert() संदेश के साथ एक छोटा popup dialog खोलता है जो पूरे page को तब तक रोक देता है जब तक visitor OK न दबाए, जो अत्यावश्यक सूचनाओं के लिए उपयोगी है पर रोज़मर्रा के output के लिए बाधाकारी।
उदाहरण: Using window.alert()
window.alert("This is an urgent message!");
document.write() का उपयोग
document.write() text को सीधे HTML document में लिखता है जब वह load हो रहा हो, एक तकनीक जो अधिकांश आधुनिक तरीकों से पुरानी है और अब साधारण उदाहरणों से आगे किसी भी काम के लिए पुरानी मानी जाती है।
उदाहरण: Using document.write()
document.write("This text is written directly into the page.");
console.log() का उपयोग
console.log() संदेशों को browser के developer console में print करता है, जो केवल developers को दिखने वाला औज़ार है, और visitors जो देखते हैं उसे प्रभावित किए बिना values जाँचने और code को debug करने का मानक तरीका है।
उदाहरण: Using console.log()
console.log("Check the browser console to see this message.");
सही Output Method चुनना
सही output method चुनना श्रोता पर निर्भर करता है: जो सामग्री visitors को दिखनी चाहिए उसके लिए innerHTML, alert केवल किसी ऐसी अत्यावश्यक चीज़ के लिए जो उन्हें रोकनी ज़रूरी हो, console.log केवल developer debugging के लिए, और साधारण सीखने के उदाहरणों के बाहर document.write से बचिए।
उदाहरण: Choosing the Right Output Method
console.log("Visible only to developers"); // for debugging
document.body.innerHTML = "<p>Visible to visitors</p>"; // for users
- किसी finished application में regular output के लिए alert() इस्तेमाल करना, यह user को interrupt करता है और debugging या urgent warnings के बाहर intrusive लगता है।
- page पहले से load होने के बाद document.write() इस्तेमाल करना, जो पूरे existing page content को अनजाने में मिटा सकता है।
- यह भूल जाना कि console.log() का output सिर्फ developers को दिखता है, regular visitors को कभी नहीं, इसलिए यह visible feedback की जगह नहीं ले सकता।
- innerHTML page पर किसी element के असली visible content को बदलता है।
- alert() और console.log() debugging और quick testing के लिए सबसे अच्छे हैं, regular user-facing output के लिए नहीं।
- document.write() ज़्यादातर एक legacy tool है और load होने के बाद इस्तेमाल किए जाने पर किसी page का content गलती से मिटा सकता है।
innerHTML, alert, document.write, और console.log सभी आज इस्तेमाल हो रहे हर browser द्वारा supported हैं, desktop से mobile तक।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: