问题描述
我想检查一个数字是否是一个可接受大小的 BigInt if 语句
我知道有解决方案
function autocomplete() {
animal = document.getElementById("animal").value;
if (animal == "alli") {
animal = "alligator";
}
if (animal == "hipp") {
animal = "hippopotamus";
}
if (animal == "ost") {
animal = "ostrich";
}
document.getElementById("animal").value = animal;
}
但我想直接在我的 if 语句中实现测试,而不是在我的函数中
有人可以帮我吗?
解决方法
您可以使用 typeof
。如果数字是 BigInt,则 typeof
将是“bigint”。否则,它将是“数字”
var numbers = [7,BigInt(7),"seven"];
numbers.forEach(num => {
if (typeof num === "bigint") {
console.log("It's a BigInt!");
} else if (typeof num === "number") {
console.log("It's a number!");
} else {
console.log("It's not a number or a BigInt!");
}
});
如果我很好地理解了您的问题,三元运算符可以帮助您:
...
const isTernary = BigInt(x) === x ? true : false
...
根据您的数字是否为 bigint,您将在变量“isTernay”中设置为 true 或 false。
并且 BigInt 对象并不严格等于 Number,而是可以在弱相等的意义上。
0n === 0
// ↪ false
0n == 0
// ↪ true
访问:https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/BigInt 了解更多关于 BigInt