truthiness

Why does {} == false throw an exception?

╄→尐↘猪︶ㄣ 提交于 2020-01-19 05:37:28
问题 In IE and Chrome, typing this into the JavaScript console throws an exception: {} == false // "SyntaxError: Unexpected token ==" However, all of these statements are evaluated with no problem: false == {} // false ({} == false) // false var a = {}; a == false // false Is this intentional behavior? Why does this happen? 回答1: In the console, when you start a statement with {} , you are not creating an object literal, but a code block (i.e. the same block as you would make with an if statement

Why does … == True return False in Python 3?

一曲冷凌霜 提交于 2020-01-12 04:48:09
问题 I am learning python, but I'm a bit confused by the following result. In [41]: 1 == True Out[41]: True In [42]: if(1): ...: print('111') ...: 111 In [43]: ... == True Out[43]: False <===== why this is False while '1 == True' is True in previous sample In [44]: if (...): <==== here ... just behaves like True ...: print('...') ...: ... According to the documentation, ... has a truth value of True. But I still feel the above code a bit inconsistent. ...And something more interesting: In [48]: 2=

if(negetive number) is true? Is something wrong with js?

两盒软妹~` 提交于 2020-01-11 11:34:08
问题 Is something wrong with js? if("hello".indexOf("world")) { // I forgot to add > -1 here console.log("hello world"); } Basically if(-1) is true. How is this possible? It took me a whole day to fix this. Is there a list available where these kind of things are listed? Or tools available to catch things like these. 回答1: As per ECMA 5.1 Standard Specifications, the following table is used to determine the truthyness of an expression +---------------------------------------------------------------

Why does truth && “string” return “string”

依然范特西╮ 提交于 2020-01-01 10:09:16
问题 Let's say I have a something like true && true #=> true Which makes sense, so I try something like: true && "dsfdsf" #=> "dsfdsf" Which surprises me because often times I'll do something like if something && something and I always thought that that was evaluating to true and would return true. Further experimentation doing things like: jruby-1.7.3 :009 > "ad" && "dsf" => "dsf" jruby-1.7.3 :010 > "ad" && "sdfd" && nil => nil jruby-1.7.3 :011 > "ad" && nil && "sdf" => nil makes it seem like

How can I avoid truthiness in Ruby?

笑着哭i 提交于 2019-12-29 08:52:13
问题 Is there any standard way to avoid truthiness in Ruby, or would I need to roll my own solution, such as class FalseClass def to_bool self end end class TrueClass def to_bool self end end true.to_bool # => true false.to_bool # => false nil.to_bool # => NoMethodError 42.to_bool # => NoMethodError Background: I know that to_bool would go against the permissive nature of Ruby, but I'm playing around with ternary logic, and want to avoid accidentally doing something like require "ternary_logic" x

Understanding JavaScript hoisting and truthy & falsy

别说谁变了你拦得住时间么 提交于 2019-12-29 00:35:55
问题 I've been reading about JavaScript hoisting sometime back. JavaScript Scoping and Hoisting by Ben Cherry Two words about “hoisting” by Dmitry Soshnikov and, some more about JavaScript type-coercion, truth & false test: Truth, Equality and JavaScript and some other resource And while practicing some, and found I m missing some important concept about the hoisting and a variable' truthy & falsy. 1: 'if' truth test with duplicate variable declaration var foo = 1; function bar() { if (!foo) {

How does Perl 6 evaluate truthiness?

假装没事ソ 提交于 2019-12-23 06:48:04
问题 In reading about Perl 6, I see a feature being trumpeted about, where you no longer have to do: return "0 but true"; ...but can instead do: return 0 but True; If that's the case, how does truth work in Perl 6? In Perl 5, it was pretty simple: 0, "", and undef are false, everything else is true. What are the rules in Perl 6 when it comes to boolean context? 回答1: Perl 6 evaluates truth now by asking the object a question instead of looking at its value. The value is not the object. It's

How does Perl 6 evaluate truthiness?

左心房为你撑大大i 提交于 2019-12-23 06:48:03
问题 In reading about Perl 6, I see a feature being trumpeted about, where you no longer have to do: return "0 but true"; ...but can instead do: return 0 but True; If that's the case, how does truth work in Perl 6? In Perl 5, it was pretty simple: 0, "", and undef are false, everything else is true. What are the rules in Perl 6 when it comes to boolean context? 回答1: Perl 6 evaluates truth now by asking the object a question instead of looking at its value. The value is not the object. It's

Using conditional statement to subtract scalar from pandas df column gives ValueError: The truth value of a Series is ambiguous

醉酒当歌 提交于 2019-12-12 20:09:35
问题 I'm trying to execute: if df_trades.loc[:, 'CASH'] != 0: df_trades.loc[:, 'CASH'] -= commission and then I get the error. df_trades.loc[:, 'CASH'] is a column of floats. I want to subtract the scalar commission from each entry in that column. For example, df_trades.loc[:, 'CASH'] prints out 2011-01-10 -2557.0000 2011-01-11 0.0000 2011-01-12 0.0000 2011-01-13 -2581.0000 If commission is 1 , I want the result: 2011-01-10 -2558.0000 2011-01-11 0.0000 2011-01-12 0.0000 2011-01-13 -2582.0000 回答1:

Understanding truthiness of python strings

不想你离开。 提交于 2019-12-11 13:18:31
问题 I understand that Python built-in types have a "truthiness" value, and the empty string is considered False , while any non-empty string is considered True . This makes sense I can check this using the built-in function bool . >>> bool("") False >>> bool("dog") True I can also make use of these truthiness values when using conditionals. For example: >>> if "dog": ... print("yes") ... yes This is confusing This doesn't work with the == operator though: >>> "dog" == True False >>> "dog" ==