javascript - why js控制台console总是会输出undefined?
高洛峰
高洛峰 2017-04-10 15:56:34
[JavaScript讨论组]

RT

js控制台console.log输出,为什么一直会有undefined结果?

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(2)
黄舟

之前回答过同样的问题,题主可参考:

http://segmentfault.com/q/1010000003821273/a-1020000003821281

简言之控制台会返回给你上一段代码的最后返回的执行结果

function a() {return 2;}
var b = 3
>> undefined
a()
>> 2
a();b;
>> 3
黄舟

If you're running console.log() from a JS file, this undefined line should not be appended.

If you're running console.log() from the console itself, it makes sense. This is why: In the console you can type a name of a variable (for example try typing window) and it prints info about it. When you run any void function (like console.log) from the console, it also prints out info about the return value, undefined in this case.

I tested both cases on my Chrome (Mac ver 23.0.1271.101) and indeed I see the undefined line when I run it inside the console. This undefined also appears when I write this line in the console: var bla = "sdfdfs"

answered Jan 31 '13 at 19:57

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号