简体   繁体   English

JavaScript条件空字符串

[英]Javascript conditional empty string

I would like to validate a date (passed in arg as a string formatted like 99/99/9999). 我想验证一个日期(以arg形式像99/99/9999这样的字符串形式传递)。 For my needs, empty/null/undefined string should be considered as good. 对于我的需求,空/空/未定义的字符串应该被认为是很好的。


Here is my validation function: 这是我的验证功能:

function isDateValid(date) {
    if(date && date.length == 0){
        return true;
    }
    //more checks
}

I got strange results, that I can't explain: 我得到了奇怪的结果,我无法解释:

  • With a empty string (string with 0 char length) the condition is evaluated to an empty String and therefore don't reach the return true 对于空字符串(字符长度为0的字符串),条件将被评估为空字符串,因此不会return true

  • With a filled string (ex: "01/01/2018") the condition is evaluated to a false boolean. 使用填充的字符串(例如:“ 01/01/2018”),条件将被评估为false布尔值。

I know how to change to make the code run as I need.... but this makes me sick to understand why this has 2 different comportement (once string, once boolean). 我知道如何进行更改以使代码按我的需要运行....但是这让我很不明白为什么会有两种不同的用法(一次字符串,一次布尔值)。

I need a captain here! 我需要一个队长!


Adding some console.log as 添加一些console.log

function isDateValid(date) {
    console.log(date);
    console.log(date.length);
    console.log(date && date.length == 0);
    console.log(typeof (date && (date.length == 0)));
    if(date && date.length == 0){
        return true;
    }
    //more...
}

will produce the following output: 将产生以下输出:

与01/01/2018 用空字符串

Here is a fiddle https://jsfiddle.net/v8easudj/2/ 这是一个小提琴https://jsfiddle.net/v8easudj/2/

JavaScript has a notion of truthy and falsy coercion so "" (empty string), null and undefined (as well as a few others) are falsey, so JavaScript具有真实虚假 强制的概念,因此“”(空字符串),null和undefined(以及其他一些)是假的,因此

if (!date)
    return true;

Will simultaneously check if date is null, undefined or an empty string and return true. 将同时检查date是否为null,未定义或为空字符串,并返回true。

For more information check out the articles on MDN 有关更多信息,请查看有关MDN的文章

As other answers stated, JavaScript has a notion of truthy and falsy values -- ie values that are implicitly considered true or false when you use them as booleans (like in if statements or with conditional operators). 正如其他答案所述,JavaScript具有真实值和虚假值的概念,即当您将它们用作布尔值时(例如在if语句中或在条件运算符中),它们被隐式地视为truefalse

In particular, empty strings are falsy. 特别是,空字符串是虚假的。


In your case, you get a different typeof , because the && boolean operator gets short-circuited on a falsy value. 在您的情况下,您将获得不同的typeof ,因为&&布尔运算符在虚假值上短路。 It stops further evaluation and returns the left operand. 它停止进一步求值并返回左操作数。

So you feed in an empty string, && stops there and you get the typeof of what's on the left -- ie of the empty string. 所以,你在一个空字符串喂, &&停在那里,你会得到typeof一个什么样的左边-即空字符串。

You feed in a non-empty string, && proceeds and you get the typeof of its right operand -- ie of the boolean result of date.length == 0 . 您输入一个非空字符串, &&继续,您将获得其正确操作数的typeof ,即date.length == 0布尔结果。

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

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