javascript 布爾_JavaScript布爾說明-如何在JavaScript中使用布爾

javascript 布爾

布爾型 (Boolean)

Booleans are a primitive datatype commonly used in computer programming languages. By definition, a boolean has two possible values: true or false.

布爾值是計算機編程語言中常用的原始數據類型。 根據定義,布爾值有兩個可能的值: truefalse

In JavaScript, there is often implicit type coercion to boolean. If for example you have an if statement which checks a certain expression, that expression will be coerced to a boolean:

在JavaScript中,布爾值通常存在隱式類型強制。 例如,如果您有一個if語句檢查某個表達式,則該表達式將被強制轉換為布爾值:

const a = 'a string';
if (a) {console.log(a); // logs 'a string'
}

There are only a few values that will be coerced to false:

只有少數幾個值會被強制設置為false:

  • false (not really coerced as it already is false)

    假(因為已經是假,所以沒有被強制)
  • null

    空值
  • undefined

    未定義
  • NaN

    N
  • 0

    0
  • "" (empty string)

    “”(空字符串)

All other values will be coerced to true. When a value is coerced to a boolean, we also call that either ‘falsy’ or ‘truthy’.

所有其他值將被強制為true。 當值強制為布爾值時,我們也稱其為“ falsy”或“ truthy”。

One way that type coercion is used is with the use of the or (||) and and (&&) operators:

使用類型強制的一種方式是使用or( || )和and( && )運算符:

const a = 'word';
const b = false;
const c = true;
const d = 0
const e = 1
const f = 2
const g = nullconsole.log(a || b); // 'word'
console.log(c || a); // true
console.log(b || a); // 'word'
console.log(e || f); // 1
console.log(f || e); // 2
console.log(d || g); // null
console.log(g || d); // 0
console.log(a && c); // true
console.log(c && a); // 'word'

As you can see, the or operator checks the first operand. If this is true or truthy, it returns it immediately (which is why we get ‘word’ in the first case & true in the second case). If it is not true or truthy, it returns the second operand (which is why we get ‘word’ in the third case).

如您所見, or運算符檢查第一個操作數。 如果這是對還是錯,它會立即返回它(這就是為什么我們在第一種情況下得到“單詞”而在第二種情況下得到true的原因)。 如果它不是對或不對,則返回第二個操作數(這就是為什么在第三種情況下我們得到“單詞”的原因)。

With the and operator it works in a similar way, but for ‘and’ to be true, both operands need to be truthy. So it will always return the second operand if both are true/truthy, otherwise it will return false. That is why in the fourth case we get true and in the last case we get ‘word’.

使用and運算符,其工作方式類似,但是要使“ and”為真,兩個操作數都必須為真。 因此,如果兩個操作數均為true / true,它將始終返回第二個操作數,否則將返回false。 這就是為什么在第四種情況下我們為真,而在最后一種情況下我們為“單詞”的原因。

布爾對象 (The Boolean Object)

There is also a native JavaScript object that wraps around a value. The value passed as the first parameter is converted to a boolean value, if necessary. If value is omitted, 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object or the string “false”, create an object with an initial value of true.

還有一個原生JavaScript對象,它包裝了一個值。 如果需要,作為第一個參數傳遞的值將轉換為布爾值。 如果省略value,0,-0,null,false,NaN,undefined或空字符串(“”),則對象的初始值為false。 所有其他值,包括任何對象或字符串“ false”,都會創建一個初始值為true的對象。

Do not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.

不要將原始布爾值true和false與布爾對象的true和false值混淆。

更多細節 (More Details)

Any object whose value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. If true, this will execute the function. For example, the condition in the following if statement evaluates to true:

值不為undefined或null的任何對象(包括值為false的布爾對象)在傳遞給條件語句時都將計算為true。 如果為true,則將執行該功能。 例如,以下if語句中的條件評估為true:

const x = new Boolean(false);
if (x) {// this code is executed
}

This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to false:

此行為不適用于布爾基元。 例如,以下if語句中的條件評估為false:

const x = false;
if (x) {// this code is not executed
}

Do not use a Boolean object to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:

不要使用布爾對象將非布爾值轉換為布爾值。 而是使用Boolean作為執行此任務的函數:

const x = Boolean(expression);     // preferred
const x = new Boolean(expression); // don't use

If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.

如果指定任何對象(包括值為false的布爾對象)作為布爾對象的初始值,則新的布爾對象的值為true。

const myFalse = new Boolean(false);   // initial value of false
const g = new Boolean(myFalse);       // initial value of true
const myString = new String('Hello'); // string object
const s = new Boolean(myString);      // initial value of true

Do not use a Boolean object in place of a Boolean primitive.

不要使用布爾對象代替布爾基元。

翻譯自: https://www.freecodecamp.org/news/booleans-in-javascript-explained-how-to-use-booleans-in-javascript/

javascript 布爾

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390754.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390754.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390754.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何進行數據分析統計_對您不了解的數據集進行統計分析

如何進行數據分析統計Recently, I took the opportunity to work on a competition held by Wells Fargo (Mindsumo). The dataset provided was just a bunch of numbers in various columns with no indication of what the data might be. I always thought that the analys…

經典:區間dp-合并石子

題目鏈接 :http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid737 這個動態規劃的思是,要得出合并n堆石子的最優答案可以從小到大枚舉所有石子合并的最優情況,例如要合并5堆石子就可以從,最優的23和14中得到最佳的答案。從兩堆…

常見排序算法_解釋的算法-它們是什么以及常見的排序算法

常見排序算法In its most basic form, an algorithm is a set of detailed step-by-step instructions to complete a task. For example, an algorithm to make coffee in a french press would be:在最基本的形式中,算法是一組完成任務的詳細分步說明。 例如&…

020-Spring Boot 監控和度量

一、概述 通過配置使用actuator查看監控和度量信息 二、使用 2.1、建立web項目&#xff0c;增加pom <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> 啟動項目&a…

matplotlib布局_Matplotlib多列,行跨度布局

matplotlib布局For Visualization in Python, Matplotlib library has been the workhorse for quite some time now. It has held its own even after more nimble rivals with easier code interface and capabilities like seaborn, plotly, bokeh etc. have arrived on the…

Hadoop生態系統

大數據架構-Lambda Lambda架構由Storm的作者Nathan Marz提出。旨在設計出一個能滿足實時大數據系統關鍵特性的架構&#xff0c;具有高容錯、低延時和可擴展等特性。Lambda架構整合離線計算和實時計算&#xff0c;融合不可變性&#xff08;Immutability&#xff09;&#xff0c…

javascript之 原生document.querySelector和querySelectorAll方法

querySelector和querySelectorAll是W3C提供的 新的查詢接口&#xff0c;其主要特點如下&#xff1a; 1、querySelector只返回匹配的第一個元素&#xff0c;如果沒有匹配項&#xff0c;返回null。 2、querySelectorAll返回匹配的元素集合&#xff0c;如果沒有匹配項&#xff0c;…

RDBMS數據定時采集到HDFS

[toc] RDBMS數據定時采集到HDFS 前言 其實并不難&#xff0c;就是使用sqoop定時從MySQL中導入到HDFS中&#xff0c;主要是sqoop命令的使用和Linux腳本的操作這些知識。 場景 在我們的場景中&#xff0c;需要每天將數據庫中新增的用戶數據采集到HDFS中&#xff0c;數據庫中有tim…

單詞嵌入_神秘的文本分類:單詞嵌入簡介

單詞嵌入Natural language processing (NLP) is an old science that started in the 1950s. The Georgetown IBM experiment in 1954 was a big step towards a fully automated text translation. More than 60 Russian sentences were translated into English using simple…

使用Hadoop所需要的一些Linux基礎

Linux 概念 Linux 是一個類Unix操作系統&#xff0c;是 Unix 的一種&#xff0c;它 控制整個系統基本服務的核心程序 (kernel) 是由 Linus 帶頭開發出來的&#xff0c;「Linux」這個名稱便是以 「Linus’s unix」來命名的。 Linux泛指一類操作系統&#xff0c;具體的版本有&a…

python多項式回歸_Python從頭開始的多項式回歸

python多項式回歸Polynomial regression in an improved version of linear regression. If you know linear regression, it will be simple for you. If not, I will explain the formulas here in this article. There are other advanced and more efficient machine learn…

《Linux命令行與shell腳本編程大全 第3版》Linux命令行---4

以下為閱讀《Linux命令行與shell腳本編程大全 第3版》的讀書筆記&#xff0c;為了方便記錄&#xff0c;特地與書的內容保持同步&#xff0c;特意做成一節一次隨筆&#xff0c;特記錄如下&#xff1a; 《Linux命令行與shell腳本編程大全 第3版》Linux命令行--- Linux命令行與she…

徹底搞懂 JS 中 this 機制

徹底搞懂 JS 中 this 機制 摘要&#xff1a;本文屬于原創&#xff0c;歡迎轉載&#xff0c;轉載請保留出處&#xff1a;https://github.com/jasonGeng88/blog 目錄 this 是什么this 的四種綁定規則綁定規則的優先級綁定例外擴展&#xff1a;箭頭函數this 是什么 理解this之前&a…

?如何在2分鐘內將GraphQL服務器添加到RESTful Express.js API

You can get a lot done in 2 minutes, like microwaving popcorn, sending a text message, eating a cupcake, and hooking up a GraphQL server.您可以在2分鐘內完成很多工作&#xff0c;例如微波爐爆米花&#xff0c;發送短信&#xff0c; 吃蛋糕以及連接GraphQL服務器 。 …

leetcode 1744. 你能在你最喜歡的那天吃到你最喜歡的糖果嗎?

給你一個下標從 0 開始的正整數數組 candiesCount &#xff0c;其中 candiesCount[i] 表示你擁有的第 i 類糖果的數目。同時給你一個二維數組 queries &#xff0c;其中 queries[i] [favoriteTypei, favoriteDayi, dailyCapi] 。 你按照如下規則進行一場游戲&#xff1a; 你…

回歸分析_回歸

回歸分析Machine learning algorithms are not your regular algorithms that we may be used to because they are often described by a combination of some complex statistics and mathematics. Since it is very important to understand the background of any algorith…

ruby nil_Ruby中的數據類型-True,False和Nil用示例解釋

ruby niltrue, false, and nil are special built-in data types in Ruby. Each of these keywords evaluates to an object that is the sole instance of its respective class.true &#xff0c; false和nil是Ruby中的特殊內置數據類型。 這些關鍵字中的每一個都求值為一個對…

淺嘗flutter中的動畫(淡入淡出)

在移動端開發中&#xff0c;經常會有一些動畫交互&#xff0c;比如淡入淡出,效果如圖&#xff1a; 因為官方封裝好了AnimatedOpacity Widget&#xff0c;開箱即用&#xff0c;所以我們用起來很方便&#xff0c;代碼量很少&#xff0c;做少量配置即可&#xff0c;所以&#xff0…

數據科學還是計算機科學_何時不使用數據科學

數據科學還是計算機科學意見 (Opinion) 目錄 (Table of Contents) Introduction 介紹 Examples 例子 When You Should Use Data Science 什么時候應該使用數據科學 Summary 摘要 介紹 (Introduction) Both Data Science and Machine Learning are useful fields that apply sev…

空間復雜度 用什么符號表示_什么是大O符號解釋:時空復雜性

空間復雜度 用什么符號表示Do you really understand Big O? If so, then this will refresh your understanding before an interview. If not, don’t worry — come and join us for some endeavors in computer science.您真的了解Big O嗎&#xff1f; 如果是這樣&#xf…