assert

assert

assert

Function

assert is used to assert that a condition is true. When the condition is true, execution continues and returns true; when the condition is false, a runtime error is thrown and the current execution is interrupted.

Syntax

Parameters

ParametersTypeRequiredDefault valueDescription
conditionAnyYesNoneThe condition to be judged is processed according to BT's if truth rule.
messageAnyNoNoneCustomized failure prompt, which will be converted to string output.

Return value

TypeDescription
BoolReturns true when the condition is true; it will not return when the condition is not true, and a runtime error will be thrown directly.

Code Example

On failure:

will throw an error similar to:

Notes

  • assert requires at least 1 argument.
  • The truth rules are consistent with the if condition, null, empty, 0, empty strings, empty arrays, and empty objects are all considered false.
  • When the assertion fails, the current assert(...) source code statement will be included in the error message as much as possible; if message is passed in, the custom message will be used as the main error message.