• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

JavaScript调试利器console.log()

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


简介

Almost all web browsers and JavaScript environments support writing messages to a console using a suite of logging methods. The most common of these is the `console.log()` method.


起步

Open up the JavaScript Console in your browser, type the following, and press Enter:

console.log("Hello, World!");

In the example above, the console.log() function prints Hello, World! to the console, along with a new line, and returns undefined (shown above in the console output window), because it has no explicit return value.

The console.log() function is predominantly used for debugging purposes.


输出变量

console.log() can be used to log variables of any kind, not only strings. Just pass the variable that you want to be displayed in the console, for example:

var foo = "bar";  console.log(foo);

Will log in the console…

If you want to log two or more values, simply separate them with commas:

console.log("thisVar:", thisVar, "and that", thatVar);


输出对象

Below depicts the result of logging an object, which can be useful, for example, to log JSON responses received from an API call:

console.log({'Email': '', 'Groups': {}, 'Id': 33, 'IsHiddenInUI': false, 'IsSiteAdmin': false, 'LoginName': 'i:0#.w|virtualdomain\user2', 'PrincipalType': 1, 'Title': 'user2'});

Will log in the console…


输出函数

The console can also be used to show the return value of a function. In the example below, isNaN() will be executed and the value returned by this function will be shown in the console;

console.log also has the ability to display function definitions by passing a non invoked instance of the function.

var func = function(){console.log("hello from func");} console.log(func.toString());

Will log to the console…


输出HTML元素

You have the ability to log any element which exists within the DOM. In this case we log the body element;

console.log(document.body);

Will log to the console…


鲜花

握手

雷人

路过

鸡蛋
专题导读
上一篇:
PHP编程常见问题集锦【四】发布时间:2022-05-14
下一篇:
Python命令行IMDB爬虫(Scraper)发布时间:2022-05-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap