[Code Example] how to test code in javascript

Total
0
Shares

In order to test code in javascript, we can use assert() method. It works in the same way as the testing in any other programming language. In this article we will show you the code to get you ready for it.

Code Example –

const errorMsg = 'the # is not even';

for (let number = 2; number <= 5; number += 1) {
    console.log(`the # is ${number}`);
    console.assert(number % 2 === 0, {number: number, errorMsg: errorMsg});
}


// output:
// the # is 2
// the # is 3
// Assertion failed: {number: 3, errorMsg: "the # is not even"}
// the # is 4
// the # is 5
// Assertion failed: {number: 5, errorMsg: "the # is not even"}

Source: MDN

Sign Up for Our Newsletters

Get only the best articles delivered to your mail

You May Also Like