How can you handle errors in JavaScript using try and catch?

Use try to wrap code that may throw an error, and catch to handle the error gracefully, preventing crashes.

try {
  let result = someFunction();
} catch (error) {
  console.error("Error:", error);
}