Questions tagged [truthiness]

In dynamically typed language, truthiness is a term used to describe a value that might evaluate to boolean true.

99 questions
43
votes
3 answers

Truth value of empty set

I am interested in the truth value of Python sets like {'a', 'b'}, or the empty set set() (which is not the same as the empty dictionary {}). In particular, I would like to know whether bool(my_set) is False if and only if the set my_set is…
Peter Thomassen
  • 1,671
  • 1
  • 16
  • 28
29
votes
2 answers

Is !! a best practice to check a truthy value in an if statement

In angular.js, there are some code snippets use !! to check whether a value is truthy in if condition. Is it a best practice? I fully understand in return value or other assignment !! is used to make sure the type is Boolean. But is it also true for…
Bargitta
  • 2,266
  • 4
  • 22
  • 35
27
votes
6 answers

How does Perl 6 evaluate truthiness?

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,…
raldi
  • 21,344
  • 33
  • 76
  • 86
24
votes
1 answer

Why does {} == false throw an exception?

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…
Jonn
  • 1,594
  • 1
  • 14
  • 25
20
votes
4 answers

Truth value of numpy array with one falsey element seems to depend on dtype

import numpy as np a = np.array([0]) b = np.array([None]) c = np.array(['']) d = np.array([' ']) Why should we have this inconsistency: >>> bool(a) False >>> bool(b) False >>> bool(c) True >>> bool(d) False
wim
  • 338,267
  • 99
  • 616
  • 750
14
votes
4 answers

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

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…
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
12
votes
2 answers

What are the truthy and falsy values in Raku?

While it is always possible to use mixins or method overrides to modify the Bool coercions, by default what values are considered to be truthy and what values are considered to be falsy? Note: this question was asked previously, but unfortunately it…
user0721090601
  • 5,276
  • 24
  • 41
12
votes
4 answers

In ruby, is truthiness idiomatic for a method name ending with a question mark?

Is it normal for methods with a question mark to return something that's truthy (for example, a number) to indicate that something is true, or should true itself be returned? Are there any examples of truthiness being used in the Ruby standard…
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
11
votes
2 answers

Is the truthiness of a dualvar always that of its string part?

The empirical behaviour of my Perl 5.26.2 x64 (Cygwin) is that a dualvar is truthy if and only if its string part is truthy: # Falsy number, truthy string => truthy $ perl -MScalar::Util=dualvar -E 'my $v=dualvar 0, "foo"; say "yes" if $v' yes #…
cxw
  • 16,685
  • 2
  • 45
  • 81
8
votes
2 answers

What are the true and false criteria for a python object?

I have seen the following cases: >>> def func(a): ... if a: ... print("True") ... >>> a = [1, 2, 3] >>> func(a) True >>> a == True False Why does this difference occur?
SeokJun Yeom
  • 401
  • 1
  • 4
  • 9
7
votes
1 answer

Groovy CompileStatic on Android messes up Groovy Truth

In Groovy it is possible to test collections for null and empty simply by placing the variable by itself inside if like: def collection = [ 'test' ] if(!collection) { //Collection is either null or empty, handle exceptional business…
MrPlow
  • 1,295
  • 3
  • 26
  • 45
7
votes
6 answers

How does (A == B == C) comparison work in JavaScript?

I was expecting the following comparison to give an error: var A = B = 0; if(A == B == 0) console.log(true); else console.log(false); but strangely it returns false. Even more strangely, console.log((A == B == 1)); returns true. How does…
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
7
votes
2 answers

Why can't SplFileInfo be converted to boolean?

One of the limitations of PHP is that objects always evaluate to true. However SplFileinfo (and subclasses such as Symfony's UploadedFile) behave differently: $a = new ArrayIterator(); // or any other class $b = new SplFileInfo(__FILE__); //…
Tamlyn
  • 22,122
  • 12
  • 111
  • 127
6
votes
2 answers

Understanding JavaScript hoisting and truthy & falsy

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…
manikanta
  • 8,100
  • 5
  • 59
  • 66
6
votes
2 answers

Why is NotImplemented truthy in Python 3?

This question is spurred from the answers and discussions of this question. The following snippet shows the crux of the question: >>> bool(NotImplemented) True The questions I have are the following: Why was it decided that the bool value of…
Julian
  • 1,078
  • 5
  • 17
1
2 3 4 5 6 7