关于布尔值“true”和字符串之间关系的混淆

时间:2016-12-26 07:59:48

标签: javascript

if(true){
    alert("hello");
}

if("string"){
    alert("hello");
}

上面的代码显示字符串可以用作布尔值“true”的替代。为什么这会起作用,这是否意味着布尔值“true”和字符串具有相同的数据类型?

4 个答案:

答案 0 :(得分:1)

作为陈述的string代表它的存在,换句话说,如果设置了string,它将返回true,就好像它的{{1}它将返回""

通过这种方式,您可以检查变量是否已设置

答案 1 :(得分:1)

JavaScript的条件运算符(例如2016-12-26 18:40:08.354 ParseStarterProject-Swift[73829:1561943] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath: 0x608000226400> {length = 2, path = 0 - 0}' *** First throw call stack: ( 0 CoreFoundation 0x00000001066e7d4b __exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010614921e objc_exception_throw + 48 2 CoreFoundation 0x00000001067512b5 +[NSException raise:format:] + 197 3 UIKit 0x0000000107fb0e44 -[UICollectionView scrollToItemAtIndexPath:atScrollPosition:animated:] + 224 4 ParseStarterProject-Swift 0x00000001051a963c _TFC25ParseStarterProject_Swift18PackViewController13viewDidAppearfSbT_ + 700 5 ParseStarterProject-Swift 0x00000001051a9741 _TToFC25ParseStarterProject_Swift18PackViewController13viewDidAppearfSbT_ + 49 6 UIKit 0x0000000107808a6c -[UIViewController _setViewAppearState:isAnimating:] + 945 7 UIKit 0x0000000107809388 -[UIViewController _endAppearanceTransition:] + 197 8 UIKit 0x00000001077de28d -[UIPresentationController transitionDidFinish:] + 834 9 UIKit 0x00000001079f5f83 -[_UICurrentContextPresentationController transitionDidFinish:] + 42 10 UIKit 0x00000001077e1ef0 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 183 11 UIKit 0x00000001081a356c -[_UIViewControllerTransitionContext completeTransition:] + 102 12 UIKit 0x00000001077daddc -[UITransitionView notifyDidCompleteTransition:] + 251 13 UIKit 0x00000001077daaef -[UITransitionView _didCompleteTransition:] + 1539 14 UIKit 0x00000001077dd51c -[UITransitionView _transitionDidStop:finished:] + 104 15 UIKit 0x00000001076edbd5 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 222 16 UIKit 0x00000001076ee12a -[UIViewAnimationState animationDidStop:finished:] + 136 17 QuartzCore 0x0000000107392648 _ZN2CA5Layer23run_animation_callbacksEPv + 316 18 libdispatch.dylib 0x00000001098940cd _dispatch_client_callout + 8 19 libdispatch.dylib 0x00000001098748a4 _dispatch_main_queue_callback_4CF + 406 20 CoreFoundation 0x00000001066abe49 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9 21 CoreFoundation 0x000000010667137d __CFRunLoopRun + 2205 22 CoreFoundation 0x0000000106670884 CFRunLoopRunSpecific + 420 23 GraphicsServices 0x000000010bf76a6f GSEventRunModal + 161 24 UIKit 0x0000000107660c68 UIApplicationMain + 159 25 ParseStarterProject-Swift 0x00000001051966cf main + 111 26 libdyld.dylib 0x00000001098e068d start + 1 27 ??? 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb) if-else)不要求条件属于实际a ? b : c类型,而是检查它是Boolean还是{{{ 1}} JavaScript中的值。

例如,truthyfalsy等值被视为null,因此它们将被视为0和三元运算符中的falsy

在更广泛的意义上,这可能被视为类型强制的情况,例如,请参阅this question以获取更多信息。因此,当解释器在false内部看到非布尔表达式时,它会像“{1}}一样”重写“,即调用从表达式到精确布尔类型的转换。

答案 2 :(得分:0)

if("string"){
    alert("hello");
}

因为您没有包含运算符,所以它只是检查字符串"string" == true是否存在。

答案 3 :(得分:0)

JavaScript有一个布尔类型,可能的值为true和false(两者都是关键字。)根据以下规则,任何值都可以转换为布尔值:
false,0,空字符串(&#34;&#34;),NaN,null和undefined都变为false。 所有其他值都成为现实。

您可以使用Boolean()函数显式执行此转换:

布尔(&#34;&#34); //假
布尔(234); // true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript