How does JavaScript handle browser compatibility, and what are best practices for ensuring consistent behavior across different browsers?

Dealing with JavaScript browser compatibility can be challenging, especially when features behave differently across browsers. What strategies or tools do you use to write cross-browser compatible JavaScript code? Are there specific practices, polyfills, or testing approaches that help ensure smoother compatibility across environments like Chrome, Firefox, Safari, and Edge?