← All Quizzes
What is the purpose of assertions in error handling?
Explanation
Assertions verify assumptions about program state and fail immediately (throwing an error) if the assumption is violated. They are used for checking invariants, preconditions, and postconditions that should always hold if the code is correct. Unlike error handling for expected runtime failures, assertions catch programming bugs. In production, assertions may be disabled for performance. Node.js provides console.assert, and libraries like Chai and the native assert module offer richer assertion capabilities for testing.