showing results for - "browser js assert"
Zahra
08 Jul 2016
1// Failed assertions to not error out of functions:
2function f() {
3    console.log("A")
4    console.assert(2 + 2 === 4)
5    console.log("B")
6    console.assert(2 + 2 == 5)
7    console.log("C")
8}
9f()
10// A
11// B
12// Assertion failed: console.assert
13// C (function finishes!)
14
15// Better choice:
16function assert(condition, message) {
17    if (!condition) {
18        throw new Error(message || "Assertion failed");
19    }
20}
21
22function g() {
23    console.log("A")
24    assert(2 + 2 === 4, "2 and 2 is not 4")
25    console.log("B")
26    assert(2 + 2 == 5, "2 and 2 is not 5")
27    console.log("C")
28}
29g()
30
31
32// A
33// B
34// Uncaught Error: 2 and 2 is not 5
35