搜尋
首頁CMS教程&#&按判斷是否為布林值的方法

判斷是否為布林值的方法

Aug 31, 2023 am 10:25 AM
判斷方法 (method)布林值 (boolean)是否 (whether)

判斷是否為布林值的方法

Boolean() 建構子可用於建立布林物件以及布林原始值,表示truefalse 值。

#在下面的程式碼中,我詳細介紹了 JavaScript 中布林值的建立。

範例:sample52.html

<!DOCTYPE html><html lang="en"><body><script>

	// Create a Boolean object using the new keyword and the Boolean() constructor.
	var myBoolean1 = new Boolean(false); // Using new keyword.
	console.log(typeof myBoolean1); // Logs 'object'.

	// Create a Boolean literal/primitive by directly using the number constructor without new.
	var myBoolean2 = Boolean(0); // Without new keyword.
	console.log(typeof myBoolean2); // Logs 'boolean'.

	// Create Boolean literal/primitive (constructor leveraged behind the scenes).
	var myBoolean3 = false;
	console.log(typeof myBoolean3); // Logs 'boolean'.
	console.log(myBoolean1, myBoolean2, myBoolean3); // Logs false false false.

</script></body></html>

Boolean() 參數

Boolean() 建構函式將一個參數轉換為布林值(即 truefalse)。任何非0、-0、nullfalseNaNundefined 或空字串("") 的有效JavaScript 值都將轉換為true。在以下範例中,我們建立兩個布林物件值:一個 true 和一個 false

範例:sample53.html

<!DOCTYPE html><html lang="en"><body><script>

	// Parameter passed to Boolean() = 0 = false, thus foo = false
	var foo = new Boolean(0)
	console.log(foo);

	// Parameter passed to Boolean() = Math = true, thus bar = true
	var bar = new Boolean(Math)
	console.log(bar);

</script></body></html>

當與 new 關鍵字一起使用時,來自 Boolean() 建構函數的實例會產生一個實際的複雜物件。您應該避免使用 Boolean() 建構函式建立布林值(而是使用文字/原始數字),因為存在與 typeof 運算子相關的潛在問題。 typeof 運算子將布林物件報告為“object”,而不是您可能期望的原始標籤(“boolean”)。此外,文字/原始值的寫入速度更快。


Boolean() 屬性與方法

Boolean() 物件具有以下屬性:

屬性(例如,Boolean.prototype;):

  • 原型

布林物件實例屬性和方法

布林物件實例具有以下屬性和方法(不包括繼承的屬性和方法):

實例屬性(例如,var myBoolean = false; myBoolean.constructor;):

  • 建構子

實例方法(例如,var myNumber = false; myBoolean.toString();):

  • toSource()
  • #toString()
  • #valueOf()

非原始 False 布林物件轉換為 True

Boolean() 建構函數建立的 false 布林物件(而不是原始值)是一個對象,並且物件會轉換為 true。因此,當透過 Boolean() 建構函式建立 false 布林物件時,該值本身會轉換為 true。在下面的範例中,我示範了 false 布林物件如何始終是「true」。

範例:sample54.html

<!DOCTYPE html><html lang="en"><body><script>

	var falseValue = new Boolean(false);

	console.log(falseValue); // We have a false Boolean object, but objects are truthy.

	if (falseValue) { // Boolean objects, even false Boolean objects, are truthy.
		console.log('falseValue is truthy');
	}

</script></body></html>

如果需要將非布林值轉換為布林值,只需使用Boolean() 建構函數,而不使用new 關鍵字,傳回的值將是原始值而不是布爾對象。 p>


某些事情是假的,其他一切都是真的

已經提到過,但值得再次提及,因為它與轉換有關:如果值為0、-0、nullfalseNaNundefined,或空字串( ""),就是false。如果在布林上下文中使用,除上述值之外的 JavaScript 中的任何值都會轉換為 true(即 if (true) {};)。

範例:sample55.html

<!DOCTYPE html><html lang="en"><body><script>

	// All of these return a false Boolean value.
	console.log(Boolean(0));
	console.log(Boolean(-0));
	console.log(Boolean(null));
	console.log(Boolean(false));
	console.log(Boolean(''));
	console.log(Boolean(undefined));
	console.log(Boolean(null));

	// All of these return a true Boolean value.
	console.log(Boolean(1789));
	console.log(Boolean('false')); // 'false' as a string is not false the Boolean value.
	console.log(Boolean(Math));
	console.log(Boolean(Array()));

</script></body></html>

結論

了解哪些 JavaScript 值被簡化為 false 至關重要,這樣您就知道所有其他值都被視為 true

以上是判斷是否為布林值的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
使用WordPress時的安全考慮是什麼?使用WordPress時的安全考慮是什麼?Apr 29, 2025 am 12:01 AM

TosecureaWordPresssite,followthesesteps:1)RegularlyupdateWordPresscore,themes,andpluginstopatchvulnerabilities.2)Usestrong,uniquepasswordsandenabletwo-factorauthentication.3)OptformanagedWordPresshostingorsecuresharedhostingwithawebapplicationfirewal

WordPress與其他網站構建者相比如何?WordPress與其他網站構建者相比如何?Apr 28, 2025 am 12:04 AM

WordPressExcelSoverotherWeberSiteBuilderSduetoItsflexible,可伸縮性,andopen-sourcenature.1)它'saversatilecmswithExtEnsextEnsexenSiveCustomizedOptionsVIATHEMESANDPLUGINS.2)它的alllearbutoffersbutoffersbutoffersbutoffersbutefersbuterbutfulcontrololoncemastered.3)

5個WordPress插件,供開發人員在2025年使用5個WordPress插件,供開發人員在2025年使用Apr 27, 2025 am 08:25 AM

2025年網站開發的七個必備WordPress插件 在2025年建立頂級WordPress網站需要速度,響應能力和可擴展性。 實現這種有效的實現通常取決於戰略插件的選擇。 這篇文章Highlig

您將使用WordPress做什麼?您將使用WordPress做什麼?Apr 27, 2025 am 12:14 AM

WordPresscanbeusedforvariouspurposesbeyondblogging.1)E-commerce:WithWooCommerce,itcanbecomeafullonlinestore.2)Membershipsites:PluginslikeMemberPressenableexclusivecontentareas.3)Portfoliosites:ThemeslikeAstraallowstunninglayouts.Ensuretomanageplugins

WordPress適合創建投資組合網站嗎?WordPress適合創建投資組合網站嗎?Apr 26, 2025 am 12:05 AM

是的,wordpressisisexcellentforcortingaportfoliowebsite.1)itoffersnumeroversnumeroverportfolio-spificthemeslike'astra'astra'astra'astra'astra'astra'astra'astra'astra'elementor'Enelementor'enableIntiviveSiveSign,Thoughtemanycanslowthesite.3)

使用WordPress而不是從頭開始編碼網站的優點是什麼?使用WordPress而不是從頭開始編碼網站的優點是什麼?Apr 25, 2025 am 12:16 AM

WordPressisadvantageousovercodingawebsitefromscratchdueto:1)easeofuseandfasterdevelopment,2)flexibilityandscalability,3)strongcommunitysupport,4)built-inSEOandmarketingtools,5)cost-effectiveness,and6)regularsecurityupdates.Thesefeaturesallowforquicke

是什麼使WordPress成為內容管理系統?是什麼使WordPress成為內容管理系統?Apr 24, 2025 pm 05:25 PM

WordPressIsAcmsDuetoItseAsofuse,自定義,USERMANAMECTION,SEO和COMMUNITYSUPPORT.1)ITSIMPLIFIESCONTENTMANGAMEWITHANINTUISIDERFEEFFECE.2)提供extentensiveCustomizationThroughThroughTheMesandPlugins.3)supportrobustuserrolesandplugins.4)supportrobustuserrolesandpermissions.4)增強

wordpress怎麼加評論框wordpress怎麼加評論框Apr 20, 2025 pm 12:15 PM

在 WordPress 網站上啟用評論功能,可以為訪客提供參與討論和分享反饋的平台。為此,請按照以下步驟操作:啟用評論:在儀錶盤中,導航至“設置”>“討論”,並選中“允許評論”複選框。創建評論表單:在編輯器中,單擊“添加塊”並蒐索“評論”塊,將其添加到內容中。自定義評論表單:通過設置標題、標籤、佔位符和按鈕文本來定制評論塊。保存更改:單擊“更新”以保存評論框並將其添加到頁面或文章中。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具