
布尔值是绝对值,true 或 false。这是非常明确的。 javascript 中的其他数据类型也具有这些固有值 true 和 false,但并不那么明显,因为它们看起来像 32、null、0 和“hello”,而不是 true 和 false。知道所有值都具有这些固有值意味着我们可以对通常用于布尔值的所有数据类型执行操作。这为我们在编码时提供了更多的创造力和灵活性。
当使用 if 等控制流关键字和 and (&&) 和 or (||) 等逻辑运算符时,我们使用布尔值来实现某些结果。这些布尔值可以显式地与 true 或 false 一起使用,但我们经常使用 ===、< 和 > 等比较运算符来生成它们。
如果我们不使用具有控制流或逻辑运算符的布尔值会发生什么?好吧,你很幸运!所有价值观本质上都是真或假,以帮助实现这一目标。我们可以将所有值分为两类:truthy 或 falsy.
当试图判断一个值是真值还是假值时,最好记住 假值 值,因为它们的数量有限:
立即学习“Java免费学习笔记(深入)”;
其他一切都是真实。如果您不确定某些内容是真是假,或者遇到似乎不明确的独特情况,您可以随时创建一个 if 语句来查看以下代码块中的代码是否运行。
if (23) { console.log(“truthy”); } // prints “truthy”
else { console.log(“falsy”); }
if (null) { console.log(“truthy”); }
else { console.log(“falsy”); } // prints “falsy”
将布尔值与逻辑 and (&&) 一起使用时,两个值都必须为 true 才能使逻辑运算符返回 true。否则,如果至少有一个值为 false,则返回 false。
console.log(false && false); // false console.log(true && false); // false console.log(true && true); // true
了解逻辑 and (&&) 运算符的机制可以帮助您处理真值和假值。如果左边的值为 false,则返回;否则,返回右侧的值。
console.log(0 && 1); // 0
console.log("a" && ""); // "" (an empty string)
console.log([] && [1, 2, 3]); // [1, 2, 3]
逻辑 and (&&) 运算符想要返回一个假值,并且只有在两者都为真时才返回右侧的真值。你可以这样想这两个参数:
(左侧)仅当我是虚假值时才使用我。 &&(右侧)否则就用我吧。
当将布尔值与逻辑或 (||) 一起使用时,两个值都必须为 false 才能使逻辑运算符返回 false。否则,如果至少有一个值为 true,则返回 true。
黑色全屏自适应的H5模板 HTML5的设计目的是为了在移动设备上支持多媒体。新的语法特征被引进以支持这一点,如video、audio和canvas 标记。HTML5还引进了新的功能,可以真正改变用户与文档的交互方式,包括: 新的解析规则增强了灵活性 淘汰过时的或冗余的属性 一个HTML5文档到另一个文档间的拖放功能 多用途互联网邮件扩展(MIME)和协议处理程序注册 在SQL数据库中存
56
console.log(false || false); // false console.log(true || false); // true console.log(true || true); // true
以下是逻辑 or (||) 运算符的工作原理:如果左边的值为 true,则返回它;如果左边的值为 true,则返回它;否则,返回右侧的值。
console.log(1 || 0); // 1
console.log("" || "a"); // "a"
console.log(undefined || null); // null
console.log([] || [1, 2, 3]); // []
逻辑 or (||) 运算符想要返回真值,并且仅在两者都是假值时才返回右侧的假值。你可以这样想这两个参数:
(左侧)仅当我是真实值时才使用我。 || (右侧)不然就用我吧。
假设您正在创建一个代表一个人的对象,该对象具有描述该人的属性以及使用该对象中的其他属性向其他人打招呼的函数。
function person(name) {
// if name is undefined, this.name will
// default to 'a person with no name'
this.name = name || 'a person with no name';
this.greet = function() {
console.log('hello, i am ' + this.name + '.');
};
}
// create person variables
var tyler = new person('tyler');
var mystery = new person();
// without an input, this.name defaults to the
// second option since name is undefined.
// call greet() from each person object
tyler.greet(); // "hello, i am tyler."
mystery.greet(); // "hello, i am a person with no name."
在上面的示例中,我们期望输入 name 参数,因此仅当 name 未定义(函数调用时没有参数)时才使用 or (||) 运算中的第二个值。
如果您正在创建对象并希望在创建对象之前确保拥有一定数量的输入,则可以将每个必需参数之间的逻辑 and (&&) 运算符链接在一起。
function Person(firstName, lastName, age) {
if (firstName && lastName && age) {
this.firstName = firstName;
this.lastName = lastName;
this.fullName = `${this.firstName} ${this.lastName}`;
this.age = age;
this.greet = function() {
console.log(`Hello, my name is ${this.fullName} and I'm ${this.age} years old.`);
};
}
// If any argument is missing, the object will only have this property.
else {
this.greet = function() {
console.log(`Hello, I am not a fully formed Person.`)
};
}
}
var tyler = new Person('Tyler', 'Meyer', 32);
var brad = new Person('Brad', '', 38);
tyler.greet(); // "Hello, my name is Tyler Meyer and I'm 32 years old."
brad.greet(); // "Hello, I am not a fully formed Person."
if 语句在创建完整的 person 对象之前检查每个参数的参数。如果即使有一个参数是假值,它也会使用 else 语句创建一个对象。因此,我们可以防止不完整的对象或为不完整的条目创建默认对象。
如果在提供值之前需要默认值,则逻辑 or (||) 运算符可能非常有用。如果您需要在继续之前需要多个值,逻辑 and (&&) 运算符会非常有帮助。这些只是两个示例,当您继续探索这些运算符时,您会发现除了通常检查布尔值的真或假之外,还有更多使用这些运算符的方法。在考虑使用逻辑 and (&&) 和 or (||) 时,请记住这两件事:
如果您有任何疑问,请在评论中留言。我很乐意进一步讨论这个话题。
编码愉快!
以上就是JavaScript 中逻辑与 (&&) 和或 (||) 的灵活运用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号