我不明白为什么 JavaScript 会这样工作.
I don't understand why JavaScript works this way.
console.log("1" + 1);
console.log("1" - 1);
第一行打印 11,第二行打印 0.为什么 JavaScript 将第一个作为字符串处理,将第二个作为数字处理?
The first line prints 11, and the second prints 0. Why does JavaScript handle the first as a String and the second as a number?
字符串连接是用 + 完成的,所以 Javascript 会将第一个数字 1 转换为字符串并连接1"和1"制作11".
String concatenation is done with + so Javascript will convert the first numeric 1 to a string and concatenate "1" and "1" making "11".
你不能对字符串进行减法运算,所以 Javascript 将第二个1"转换为一个数字,并从 1 中减去 1,得到零.
You cannot perform subtraction on strings, so Javascript converts the second "1" to a number and subtracts 1 from 1, resulting in zero.
这篇关于为什么 JavaScript 以不同方式处理字符串和数字之间的加号和减号运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)
XMLHttpRequest 的 getResponseHeader() 的限制?Restrictions of XMLHttpRequest#39;s getResponseHeader()?(XMLHttpRequest 的 getResponseHeader() 的限制?)