博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript 布尔_JavaScript布尔说明-如何在JavaScript中使用布尔
阅读量:2518 次
发布时间:2019-05-11

本文共 4471 字,大约阅读时间需要 14 分钟。

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 = 0const e = 1const f = 2const g = nullconsole.log(a || b); // 'word'console.log(c || a); // trueconsole.log(b || a); // 'word'console.log(e || f); // 1console.log(f || e); // 2console.log(d || g); // nullconsole.log(g || d); // 0console.log(a && c); // trueconsole.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);     // preferredconst 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 falseconst g = new Boolean(myFalse);       // initial value of trueconst myString = new String('Hello'); // string objectconst s = new Boolean(myString);      // initial value of true

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

不要使用布尔对象代替布尔基元。

翻译自:

javascript 布尔

转载地址:http://jmrwd.baihongyu.com/

你可能感兴趣的文章
HttpServletRequest /HttpServletResponse
查看>>
SAM4E单片机之旅——24、使用DSP库求向量数量积
查看>>
从远程库克隆库
查看>>
codeforces Unusual Product
查看>>
hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
查看>>
正则表达式的搜索和替换
查看>>
个人项目:WC
查看>>
地鼠的困境SSL1333 最大匹配
查看>>
flume+elasticsearch+kibana遇到的坑
查看>>
【MM系列】在SAP里查看数据的方法
查看>>
C#——winform
查看>>
CSS3 transform制作的漂亮的滚动式导航
查看>>
《小强升职记——时间管理故事书》读书笔记
查看>>
Alpha 冲刺(3/10)
查看>>
Kaldi中的Chain模型
查看>>
spring中的ResourceBundleMessageSource使用和测试示例
查看>>
css规范 - bem
查看>>
电梯调度程序的UI设计
查看>>
转自 zera php中extends和implements的区别
查看>>
Array.of使用实例
查看>>