Answers for "browser js assert"

0

browser js assert

// Failed assertions to not error out of functions:
function f() {
    console.log("A")
    console.assert(2 + 2 === 4)
    console.log("B")
    console.assert(2 + 2 == 5)
    console.log("C")
}
f()
// A
// B
// Assertion failed: console.assert
// C (function finishes!)

// Better choice:
function assert(condition, message) {
    if (!condition) {
        throw new Error(message || "Assertion failed");
    }
}

function g() {
    console.log("A")
    assert(2 + 2 === 4, "2 and 2 is not 4")
    console.log("B")
    assert(2 + 2 == 5, "2 and 2 is not 5")
    console.log("C")
}
g()


// A
// B
// Uncaught Error: 2 and 2 is not 5
Posted by: Guest on May-28-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language