Home >Web Front-end >JS Tutorial >The role of console.log()
Recommended tutorial: Javascript tutorial
console.log ()
Syntax: console.log("content");
Function: Output the "content" In the console, it is a very frequently used function to facilitate future debugging. (For the console, press F12 in the browser to open the second item (console) of the developer mode, and you can also do instant testing under the console tab)
Comparison with the alert() method:
When the alert() pop-up window appears, subsequent scripts are blocked. This means that if you need to make a loop with a relatively large value, it will not be executed in an instant. You must click on the pop-up window before it continues to execute. This testing efficiency is quite low.
Alert() will implicitly convert all content to string type (i.e. toString()), which will output content that the developer cannot predict. For example, using alert() to output object type:
var a = { myNum:1, myArr:[1,2,3] }; alert(a);
You originally hoped to get the key-value pair content in object a, but alert() will only output [object, Object].
In summary, the console.log() method is generally used for debugging instead of alert() during development.
Use case:
console.log(parseInt(Math.random()*10)); //和alert一样该方法也可以运算 console.log(a); //前文中的对象a(得到的是键值对内容) for(var i = 0; i < 6; i++){ console.log(i); } //循环5次每次输出i的数值。在控制台会瞬间出现5个数,相比alert()要方便得多
The above is the detailed content of The role of console.log(). For more information, please follow other related articles on the PHP Chinese website!