javascript 布爾
布爾型 (Boolean)
Booleans are a primitive datatype commonly used in computer programming languages. By definition, a boolean has two possible values: true
or false
.
布爾值是計算機編程語言中常用的原始數據類型。 根據定義,布爾值有兩個可能的值: true
或false
。
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 布爾