Skip to content

https://marketplace.visualstudio.com/items?itemName=ms-vscode.js-debug-nightly

console

การใช้ console.log() เป็นวิธีที่ง่ายและรวดเร็วในการตรวจสอบค่าของตัวแปรหรือผลลัพธ์ของฟังก์ชัน

javascript
let x = 5;
console.log("ค่าของ x คือ:", x);

function greet(name) {
  console.log("Hello,", name);
}
greet("Alice");

debugger

คำสั่ง debugger ใช้เพื่อหยุดการทำงานของโค้ดชั่วคราว ทำให้สามารถตรวจสอบสถานะของโปรแกรมได้

javascript
function calculateSum(a, b) {
  debugger;
  return a + b;
}

let result = calculateSum(3, 4);
console.log(result);

try...catch

การใช้ try...catch ช่วยในการจัดการข้อผิดพลาดที่อาจเกิดขึ้นในโค้ด ทำให้โปรแกรมไม่หยุดทำงานทันทีเมื่อเกิดข้อผิดพลาด

javascript
try {
  // โค้ดที่อาจเกิดข้อผิดพลาด
  let result = someUndefinedFunction();
} catch (error) {
  console.error("เกิดข้อผิดพลาด:", error.message);
}

Released under the MIT License