is a common method in JavaScript, used to output information on the console. Usage: 1. Output basic types: console.log() can directly output basic types of values, such as strings, numbers, etc.; 2. Output objects: For object type values, console.log() will output the reference of the object by default. address, rather than the actual content of the object; 3. Formatted output: the console.log() method can accept multiple parameters and format them for output; 4. Console grouping; 5. Set log level
console.log() is a common method in JavaScript, used to output information on the console.
The following is an introduction to its usage:
1. Output basic type: console.log() can directly output basic type values, such as strings, numbers, and Boolean Worth waiting. For example:
javascript
console.log("Hello, world!"); // 输出字符串 console.log(42); // 输出数字 console.log(true); // 输出布尔值
2. Output object: For values of object type, console.log() will output the reference address of the object by default, rather than the actual content of the object. In order to display the actual content of the object, it can be converted to a string using the JSON.stringify() method. For example:
javascript
let obj = { name: "John", age: 30 }; console.log(obj); // 输出对象的引用地址 console.log(JSON.stringify(obj)); // 输出对象的实际内容
3. Formatted output: The console.log() method can accept multiple parameters and format them for output. Formatting can be achieved using string interpolation or template strings. For example:
javascript
let name = "John"; let age = 30; console.log("My name is " + name + ", and I am " + age + " years old."); // 字符串插值 console.log(`My name is ${name}, and I am ${age} years old.`); // 模板字符串
4. Console grouping: Use the console.group() method to group the log information in the console for easy viewing and management. For example:
javascript
console.group("User Info"); console.log("Name: John"); console.log("Age: 30"); console.groupEnd(); // 结束分组
5. Set the log level: The console.log() method defaults to output information at the "information" level. You can also use other methods, such as console.debug(), console.warn(), console.error(), etc., to set different log levels. For example:
javascript
console.debug("This is a debug message."); // 输出调试级别信息 console.warn("This is a warning message."); // 输出警告级别信息 console.error("This is an error message."); // 输出错误级别信息
The above is the detailed content of Introduction to console.log usage. For more information, please follow other related articles on the PHP Chinese website!