← Back to JavaScript Course | Chapter 12: Reference & Interview | Lesson 2 of 9

JS डिबगिंग

Debugging आपके program में गलतियां ढूंढना और ठीक करना है, जैसे कोई detective होना। console messages और breakpoints जैसे tools आपको रुककर यह देखने देते हैं कि क्या हो रहा है।
Syntax
javascript
console.log(value);
debugger;
console.error(value);
console.table(data);

Debugging क्या है

Debugging यह पद्धतिगत तरीके से ढूंढने की प्रक्रिया है कि code उम्मीद के मुताबिक व्यवहार क्यों नहीं कर रहा, आमतौर पर कारण का अंदाज़ा लगाने के बजाय execution के अलग-अलग points पर values जांचकर।

उदाहरण: What Is Debugging

javascript
const value = 5 + "5"; // unexpected result?
console.log(value); // inspect it directly instead of guessing

console Methods

console.log() जल्दी देखने के लिए values print करता है, जबकि console.table(), console.error(), और console.warn() ज़्यादा जटिल debugging sessions में आसानी से scan करने के लिए output को format या categorize करते हैं।

उदाहरण: console Methods

javascript
// Call `console.table([{ id: 1, name: "Sam" }, { id: 2, name: "Amit" }])`
// Call `console.table([{ id: 1, name: "Sam" }, { id: 2, name: "Amit" }])`
console.table([{ id: 1, name: "Sam" }, { id: 2, name: "Amit" }]);
// Print "Something went wrong" to the console as an error
// Print "Something went wrong" to the console as an error
console.error("Something went wrong");
// Print "This is deprecated" to the console as a warning
// Print "This is deprecated" to the console as a warning
console.warn("This is deprecated");

Breakpoints और Debugger

एक breakpoint execution को एक खास line पर रोक देता है ताकि आप सिर्फ print statements पर निर्भर रहने के बजाय browser या Node debugger में variables जांच सकें और code को एक-एक statement करके step through कर सकें।

उदाहरण: Breakpoints and Debugger

javascript
function calculate(a, b) {
  debugger; // pauses execution here in dev tools
  return a + b;
}
calculate(2, 3);

Debugging के दौरान Errors संभालना

error-prone code को debug करते समय, पूरे error object को catch और log करें (सिर्फ उसका message नहीं) ताकि आप वह stack trace रखें जो ठीक-ठीक दिखाता है कि failure कहाँ और कैसे शुरू हुई।

उदाहरण: Handling Errors While Debugging

javascript
try {
  JSON.parse("bad json");
} catch (error) {
  console.log(error); // log the full error object, not just error.message
}

Debugging Best Practices

bug को सबसे छोटे संभव उदाहरण से reproduce करें, कारण के बारे में एक hypothesis बनाएं, और उसे एक targeted check से verify करें — यह randomly code बदलने और फिर से चलाने से तेज़ है।

उदाहरण: Debugging Best Practices

javascript
// Reproduce with the smallest example, then verify a hypothesis:
function add(a, b) { return a + b; }
console.log(add(2, "3")); // hypothesis: string coercion is happening
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. production code में console.log या debugger statements छोड़ देना।
  2. किसी object को log करना और बाद में console के दिखाने पर भरोसा करना, क्योंकि उसे expand करने पर उसकी current values दिखती हैं, log time की values नहीं।
  3. console error में stack trace को नजरअंदाज करना, जो file और line दिखाता है।
🔒

Chapter Quiz — Complete all 9 topics to unlock

0/9 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.