Why is 'false' truthy in javascript?

前端 未结 5 1695
面向向阳花
面向向阳花 2021-01-19 10:46

I understand that an empty string is falsy in javascript and a not-empty string is truthy in javascript.

However, why is \'false\' truthy in javascript,

5条回答
  •  一整个雨季
    2021-01-19 11:14

    In Javascript any string that is not empty is truthy.

    So, when evaluating any non-empty string results in true even when the string itself is 'false'.

    You can read more about truthy and falsy values.

    If you want to check a string for truthness, you can check it's length.

    var val = str.length > 0;
    

提交回复
热议问题