4 ms·
Lua's behavior here is identical to Ruby's, where 0 is also a truthy value: $ irb irb(main):001:0> if 0 then puts "0 is truthy!" end 0 is truthy!
by graue 14y ago
Lua's behavior here is identical to Ruby's, where 0 is also a truthy value:
$ irb
irb(main):001:0> if 0 then puts "0 is truthy!" end
0 is truthy!
=> nil
irb(main):002:0> if (not 0) then puts "(not 0) is truthy" end
=> nil
irb(main):003:0> if (0 == true) then puts "0 is equal to true" end
=> nil
Many things about many programming languages are surprising to newcomers. However, the rule is very simple (in Lua): In a boolean context, every value besides nil and false is treated as though it were true. It is absolutely consistent. (a == b) is not a boolean context, or else 2 == 3 would be true.