How many ways to Display (Print) data in JavaScript?

Display

• document.write( )
• window.alert( )
• console.log( )
• innerHTML

document.write( )

This function is used to write arbitrary HTML and content into the page. If we use this function after an HTML document is fully loaded, will delete all existing HTML. It is used only for testing purposes.

Ex-

document.write(“Hello World”);
document.write(variable);
document.write(4+2);
document.write(“Hello World.”);
document.write(“Hello World. ” + variable + “ ”);

window.alert( )

This function is used to display data in alert dialog box. alert really should be used only when you truly want to stop everything and let the user know something.

Ex-

window.alert(“Hello World”);
window.alert(variable);
window.alert(4+2);
window.alert(“Hello World” + variable);

console.log( )

This function is used to display data in the console. This is used for debugging purpose. We can identify our code‟s error.

Ex-

console.log(“Hello World”);
console.log(variable);
console.log(4+2);
console.log(“Hello World” + variable);

Tagged : / / / / /