Using the Assert Helper
Using the assert() helper method
Overview
Basic Usage
function processOrder(orderId) {
// Assert that orderId is provided
assert(orderId > 0, "Order ID must be greater than 0");
var order = orderService.getById(orderId);
// Assert that order exists
assert(!isNull(order), "Order not found");
// Assert order is in correct status
assert(order.status == "pending", "Order must be pending to process");
// Process the order
return processPayment(order);
}function processOrder(orderId) {
// Assert that orderId is provided
assert(orderId > 0, "Order ID must be greater than 0");
var order = orderService.getById(orderId);
// Assert that order exists
assert(!isNull(order), "Order not found");
// Assert order is in correct status
assert(order.status == "pending", "Order must be pending to process");
// Process the order
return processPayment(order);
}Syntax
Return Values
Common Patterns
Null Checking
Range Validation
Array/Collection Validation
State Validation
API Input Validation
Assert vs Validate
Use assert() when:
assert() when:Use validate() when:
validate() when:Error Handling
Catching AssertError
Best Practices
1. Use Clear, Specific Messages
2. Fail Fast
3. Don't Overuse Assert
4. Use for Invariants
See Also
Last updated
Was this helpful?