Many developers still use alert boxes to check their JavaScript code. And yet there is a Javascript console in most browsers: Google Chrome, Firefox (with Firebug), Internet Explorer, Opera (with the Developer Console tool) and Safari.
For example in IE8, you can access IE8 script console by launching the “Developer Tools” (F12). Click the “Script” tab, then click “Console” on the right.

Here is a simple function to check if the console is turned on before writing the text. Otherwise, an alert box is displayed:

function writeInJsConsole (text) { 
    if (typeof console !== 'undefined') { 
        console.log(text);     
    } else { 
        alert(text);     
    } 
}

writeInJsConsole('toto');

But, this JS console allows other methods:

console.clear(); 
console.log("hello world from js to ie8 console"); 
console.info("sample info"); 
console.error("sample error"); 
console.warn("sample warning"); 
var x = "hello"; var y = 10; var z=1.1; 
console.log("string:%s:int:%i,real:%f",x,y,z); 

and the results in console could be: