“真实"的结果和“虚假"在 JavaScript 中令人困惑 [英] Results of "truthy" and "falsey" is confusing in JavaScript

查看:41
本文介绍了“真实"的结果和“虚假"在 JavaScript 中令人困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 JavaScript 基础知识.我对真实"和虚假"的结果感到困惑.谁能告诉我这是什么原因?

I am learning JavaScript basics. I am confused about the results of "truthy" and "falsey". Can anyone tell the reason for this?

myVariable = undefined ? "truthy" : "falsey";

结果是falsey".

myVariable = typeOf someUndefinedVariable ? "truthy" : "falsey";

结果是真实".

我期望第二个结果为falsey".为什么我得到了真相"?我真的很困惑.

I expected "falsey" instead for the second result. Why did I get "truthy"? I am really confused.

推荐答案

typeof 返回值的类型 name,它总是一个非空字符串.非空字符串真实.

typeof returns the type name of the value, which is always a non-empty string. A non-empty string is truthy.

> typeof someUndefinedVariable
"undefined"
> typeof (typeof someUndefinedVariable)
"string"
> Boolean("undefined")
true

另见 JavaScript 中的所有假值

这篇关于“真实"的结果和“虚假"在 JavaScript 中令人困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆