简体   繁体   English

如果比较(if语句)比较未定义的值,则返回true

[英]Return true if comparison (if statement) compares a undefined value

I'm working with a form that filters data. 我正在使用可过滤数据的表单。 If the user does not put in any data the id will return undefined. 如果用户未输入任何数据,则ID将返回undefined。 If not it can be used in the if statement like: if (myValue < userInputValue) which will return true/false. 如果不是,则可以在if语句中使用它,例如: if (myValue < userInputValue) ,它将返回true / false。 However if nothing is filled in it returns false (of course). 但是,如果未填写任何内容,则返回false(当然)。 Is it possible to change this to true? 是否可以将其更改为true? My alternative solution would be to check for the variables and if it is undefined assign a value myself (however choosing a max number is not very error proof): if (!userInputValue) { userInputValue = 1 } . 我的替代解决方案是检查变量,如果变量未定义,请自己分配一个值(但是,选择一个最大数字并不能很好地证明错误): if (!userInputValue) { userInputValue = 1 }

What would be the best way to tackle this problem? 解决这个问题的最佳方法是什么?

if (myValue === undefined || myValue < userInputValue)

或者,如果您希望任何虚假值触发条件,

if (!myValue || myValue < userInputValue)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM