Home  >  Article  >  Web Front-end  >  How to output content in javascript? Two common ways

How to output content in javascript? Two common ways

PHPz
PHPzOriginal
2023-04-24 10:50:364784browse

In modern web development, JavaScript is a very important programming language. It is widely used in many web pages and can achieve many interactive effects and dynamic effects. Output is one of the most basic operations in JavaScript. Two common output methods will be introduced below.

1. Use the console.log() function to output

The console.log() function is one of the commonly used output functions. It can output information on the JavaScript console and can output any Type of data, such as: string, number, array, object, etc. The following is a simple example:

console.log("Hello World!")

Hello World! will be output in the console, which shows that the console.log() function has successfully output a message.

In order to illustrate the role of console.log(), we can write a simple function to deepen our understanding:

function add(a, b) {
    console.log(a + b);
}

add(1, 2);

In this example, we wrote a function named add to receive Two parameters a and b are added together and output to the console. When we call the add function, the result 3 will be output.

2. Use the document.write() function to output

The document.write() function can write the output directly into the HTML document, so that we can see our output on the page Result. The following is a simple example:

document.write("Hello World!");

You will see the result Hello World! on the page.

Similarly, we can use a function to deepen our understanding:

function showMessage() {
    document.write("<h1>Hello World!</h1>");
}

showMessage();

In this example, we wrote a function called showMessage to output a message in an HTML page. When we call the showMessage function, a "Hello World!" message will be output on the page.

Summary

Above, we introduced two common JavaScript output methods: console.log() and document.write(). Both methods are an integral part of JavaScript programming and can help us understand the basics of JavaScript more deeply. In actual development, we can flexibly use this knowledge to achieve various interactive effects and dynamic page effects.

The above is the detailed content of How to output content in javascript? Two common ways. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn