代码如下:
function write(msg){
for(var i = 0; i document.write(arguments[i] + '
');
}
}
//关于 '&&'
test1 = 1 && 2 && 3 && 4;
test2 = '0' && 2 && 3 && 4;
test3 = 1 && 2 && 0 && 4;
test4 = 2 && 'i' && 'love' && 3 && 'you';
test5 = 'i' && 'hate' && 1 && 0 && 'you';
test6 = 1 && false && 'ihateyou' && '2';
test7 = 2 && true && 'ihatehateyou' && '23';
test8 = 4 && true && 'undefined' && 'true' && '1';
test9 = 4 && true && undefined && 'true' && '1';
test10 = 4 && true && 'null' && 'true' && '1';
test11 = 4 && true && null && 'true' && '1';
write(test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11);
write('----------------------------------------------');
//关于 '||'
_test1 = 1 || 2 || 3 || 4;
_test2 = 0 || 2 || 3 || 4;
_test3 = 0 || '0' || 8 || 4;
_test4 = 2 || 'i' || 'love' || 0 || 'you';
_test5 = 0 || 'hate' || 1 || 0 || 'you';
_test6 = false || 0 || 'ihateyou' || '2';
_test7 = false || true || 'ihatehateyou' || '23';
_test8 = 0 || 0 || 'undefined' || 'true' || '1';
_test9 = 0 || 0|| undefined || 'true' || '1';
_test10 = 0 || false || 'null' || 'true' || '1';
_test11 = 0 || 0 || null || 'true' || '1';
write(_test1, _test2, _test3, _test4, _test5, _test6, _test7, _test8, _test9, _test10, _test11);
来看看输出结果就会明白了:
关于 ‘&&'的输出结果为:
4
4
0
you
0
false
23
1
undefined
1
null
每一行的编号对应上面的每一个 test。
关于 ‘||'的输出结果为:
1
2
0
2
hate
ihateyou
true
undefined
true
null
true
仔细对照着看的话就会清楚一些了:
多个连续的 && 的表达式中若没有 0, false, undefined, null 的话,它将取得最后一个“子表达式”的值,否则将表达式中的 0, false, undefined, null 返回。
多个连续的 || 的表达式将会取第一个“子表达式”的值,若为 0, false, undefined, null 中之一的话则取下一个“子表达式”的值,以此类推,直至找到不为 0, false, undefined, null 的“子表达式”,并将它作为整个表达式的值。
补充:
上面的似乎没有考虑一种情况,就是有一个子表达式为 '' 怎么办呢?其实可以再换一种表述方式来描述 && 和 || 的工作方式:
对于 (...) && (...) && (...) ...
从左至右遍历各个子表达式,并将每个子表达式进行 Boolean 的强制转换,若出现 Boolean(子表达式) 为 false 的情况,则整个表达式的值即为此子表达式的值(0 或 false 或 undefined 或 null 或 ''),后面的子表达式不再判断;若所有的 Boolean(子表达式) 均为 true,则整个表达式的值即为最后一个子表达式的值。
对于 (...) || (...) || (...) ...
从左至右遍历各个子表达式,并将每个子表达式进行 Boolean 的强制转换,若出现 Boolean(子表达式) 为 true 的情况,则整个表达式的值即为此子表达式的值,后面的子表达式不再“理会”;若 Boolean(子表达式) 为 false,则判断后一个子表达式的 Boolean 情况,直至找到 Boolean(子表达式) 为 true 的情况;若全部的 Boolean(子表达式) 均为 false,则返回最后一个子表达式的值(0 或 false 或 undefined 或 null 或 '')。
这里要注意:
Boolean(false) != Boolean('false'),前者为 false,而后者为 true 。
Boolean(undefined) != Boolean('undefined'),前者为 false,而后者为 true 。
Boolean(null) != Boolean('null'),前者为 false,而后者为 true 。
Boolean(0) != Boolean('0'),前者为 false,而后者为 true 。
Boolean('') == false

JavaScript核心數據類型在瀏覽器和Node.js中一致,但處理方式和額外類型有所不同。 1)全局對像在瀏覽器中為window,在Node.js中為global。 2)Node.js獨有Buffer對象,用於處理二進制數據。 3)性能和時間處理在兩者間也有差異,需根據環境調整代碼。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要區別在於類型系統和應用場景。 1.Python使用動態類型,適合科學計算和數據分析。 2.JavaScript採用弱類型,廣泛用於前端和全棧開發。兩者在異步編程和性能優化上各有優勢,選擇時應根據項目需求決定。

選擇Python還是JavaScript取決於項目類型:1)數據科學和自動化任務選擇Python;2)前端和全棧開發選擇JavaScript。 Python因其在數據處理和自動化方面的強大庫而備受青睞,而JavaScript則因其在網頁交互和全棧開發中的優勢而不可或缺。

Python和JavaScript各有優勢,選擇取決於項目需求和個人偏好。 1.Python易學,語法簡潔,適用於數據科學和後端開發,但執行速度較慢。 2.JavaScript在前端開發中無處不在,異步編程能力強,Node.js使其適用於全棧開發,但語法可能複雜且易出錯。

javascriptisnotbuiltoncorc; sanInterpretedlanguagethatrunsonenginesoftenwritteninc.1)JavascriptwasdesignedAsignedAsalightWeight,drackendedlanguageforwebbrowsers.2)Enginesevolvedfromsimpleterterpretpretpretpretpreterterpretpretpretpretpretpretpretpretpretcompilerers,典型地,替代品。

JavaScript可用於前端和後端開發。前端通過DOM操作增強用戶體驗,後端通過Node.js處理服務器任務。 1.前端示例:改變網頁文本內容。 2.後端示例:創建Node.js服務器。

選擇Python還是JavaScript應基於職業發展、學習曲線和生態系統:1)職業發展:Python適合數據科學和後端開發,JavaScript適合前端和全棧開發。 2)學習曲線:Python語法簡潔,適合初學者;JavaScript語法靈活。 3)生態系統:Python有豐富的科學計算庫,JavaScript有強大的前端框架。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

Dreamweaver CS6
視覺化網頁開發工具

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用