JS: Print

By Xah Lee. Date: . Last updated: .

console.log

To print, use console.log

console.log("hello");
// print to browser's console

To see the output of console.log, open Browser Console .

alert("hello");
// bring up a pop-up dialog

console.assert

console.assert(expression)

prints nothing if expression is true. else it prints “Assertion failed”.

console.assert(3 === 3)
// prints nothing
console.assert(3 === 4)
// Assertion failed

🛑 warning: if the expression in assert may not be the boolean literal value true but still pass the test. For example, console.assert(3). Better is console.assert(3 === true)

// this passes the test. but is not what we want.
console.assert(3)

// better
console.assert(3 === true)
// Assertion failed

we use console.assert often in this tutorial to verify JavaScript behavior.