console.log有什么用?

请用一个代码示例解释如何在JavaScript中使用它。


当前回答

如果您的浏览器支持调试,您可以使用console.log()方法来显示JavaScript值。

用F12激活浏览器中的调试,并在调试器菜单中选择“控制台”。

JavaScript中的控制台。尝试修复或“调试”一个不起作用的JavaScript程序,并练习使用console.log()命令。根据你所使用的浏览器,有一些快捷方式可以帮助你访问JavaScript控制台:

Chrome控制台键盘快捷键

Windows操作系统:Ctrl + Shift + J Mac: Cmd + Option + J

Firefox控制台键盘快捷键

Windows: Ctrl + Shift + K Mac: Cmd + Option + K

Internet Explorer控制台键盘快捷键

F12键

Safari控制台键盘快捷键

Cmd + Option + C

其他回答

举个例子——假设你想知道哪行代码可以运行你的程序(在它崩溃之前!),只需输入

console.log("You made it to line 26. But then something went very, very wrong.")

A point of confusion sometimes is that to log a text message along with the contents of one of your objects using console.log, you have to pass each one of the two as a different argument. This means that you have to separate them by commas because if you were to use the + operator to concatenate the outputs, this would implicitly call the .toString() method of your object. This in most cases is not explicitly overriden and the default implementation inherited by Object doesn't provide any useful information.

在控制台尝试的例子:

>>> var myObj = {foo: 'bar'}
undefined
>>> console.log('myObj is: ', myObj);
myObj is: Object { foo= "bar"}

然而,如果你试图连接信息文本消息与对象的内容,你会得到:

>>> console.log('myObj is: ' + myObj);
myObj is: [object Object]

请记住,console。log实际上可以接受任意多的参数。

console.log()是JavaScript中的一个函数,用于打印之前在其中定义的任何类型的变量,或者只是打印需要显示给用户的任何消息。

如代码:

function func() { return (5 * 18); }
     console.log(func());

IMG:

输出:

console.log与jQuery无关。

它将消息记录到调试控制台,例如Firebug。

它用于将(传递给它的任何内容)记录到Firebug控制台。主要用途是调试JavaScript代码。