Home  >  Article  >  Web Front-end  >  How to implement console output in js

How to implement console output in js

青灯夜游
青灯夜游Original
2018-12-10 17:16:3125245browse

In js, you can use the Console object to achieve console output. Commonly used console output commands are: console.log(), console.info(), console.error(), console .warn() etc.

How to implement console output in js

#This article will introduce to you what the Console object is? What is the use? Let everyone know about common console output commands. The following is a specific introduction, I hope it will be helpful to you. [Recommended related video tutorials: JavaScript Tutorial]

Console object

The Console object is a built-in provided by the browser Object is used for JavaScript debugging and access to the debugging console. There is no Console object in native JS by default.

Common uses:

1. When an error occurs when the web page code is running, the running error message is displayed.

2. Provides a command line interface for interacting with web page code.

How to open the console:

1. Open the audit panel through the F12 key, select the console panel in the audit panel,

2. Click Right-click the mouse, select the Inspect Element (Inspection) tab, select console panel

3 in the audit panel, Google Chrome, press ctrl shift I, open the console panel.

Console output commands

Let’s take a look at the commonly used console output commands:

1 , console.log(): Output a message on the console.

Syntax:

console.log(object[, object, ...]);

Note: When there are multiple parameters, they need to be separated by spaces.

console.log() supports printf’s placeholder format output. Supported placeholders are: characters (%s), integers (%d or %i), floating point numbers (%f) and objects (%o):

How to implement console output in js

Example:

console.log("%d年%d月%d日",2018,12,10);

Output:

How to implement console output in js

##2. console.info(): Output a message on the console.

console.info() is an alias of console.log(), and its effect is similar to console.log().

Example:

console.info(“消息”);
console.info("%d年%d月%d日",2018,12,10);

How to implement console output in js

3. console.error(): Output a message representing the error

When outputting information, a red cross will be added at the front to indicate an error, and the stack where the error occurred will be displayed.

Example:

How to implement console output in js

##4, console.warn():

Output warning informationWhen outputting information , a yellow triangle with an exclamation mark will be added at the front to indicate a warning.

Example:

How to implement console output in js##5. console.assert()

: Judgment output

Syntax:

console.assert(表达式,字符串)

Only when the result of the expression is false, the string will be output, that is, an exception will be thrown; otherwise, there will be no output result.

Example:

var year = 2014;
console.assert(year == 2018,"错误" );

Rendering:


##6, console.table()How to implement console output in js: Convert composite type data to table output and display

Example:

var arr= [ 
         { num: "1"},
         { num: "2"}, 
         { num: "3" }
    ];
console.table(arr);
var obj= {
     a:{ num: "1"},
     b:{ num: "2"},
     c:{ num: "3" }
     };
console.table(obj);
Rendering:

Summary: The above is this article The entire content, I hope it will be helpful to everyone's study.

How to implement console output in js

The above is the detailed content of How to implement console output in js. 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