如何在javascript中显示输出?

有4种显示JavaScript输出的方法。

 a)使用innerHTML属性在HTML元素中显示输出。 

示例

<html>
<body>
<p id="display"></p>
<script>
   document.getElementById("display").innerHTML = 10 + 10;
</script>
</body>
</html>

输出结果

20.


b)使用document.write()显示输出。

示例

<html>
<body>
<script>
   document.write(2 + 3);
</script>
</body>
</html>

输出结果

5


c)通过console.log()显示输出。 

示例

<html>
<body>
<script>
   console.log(2 + 3);
</script>
</body>
</html>

在调试器控制台中,输出

5.


d)通过警报框显示输出。

示例

<html>
<body>
<script>
   alert(2 + 3);
</script>
</body>
</html>

在警报窗口框中,输出

5

在处理json字符串或其他大数据console.log的情况下,log是最佳选择。