Is it common for a language to evaluate undefined to be false? If so, why is this done?

UPDATE: The question remains unanswered. @Alastair_Pitts: If I am missing something, this is a two-part question. Part two: “If so, why is this being done?” and did not receive a response.


Believe me, the question is clear, but if you have any questions, just let me know. Thanks!

undefined = unknown and is a reference to a system based on ternary logic.


Help: http://en.wikipedia.org/wiki/Ternary_logic

+1
java c ++ c c # ruby
source share
5 answers

In many, if not most, the meanings of languages ​​are either false, which means that something does not exist or does not have value or truth, but means that something exists or has value. The list of false values ​​is usually: (they are evaluated as false)

  • 0 (zero, number)
  • '' (empty line)
  • null (if this value exists)
  • undefined (if this value exists)
  • false / false (if it is of type boolean)

Everything else is true and evaluates to true.

Edit: Made the answer a little less biased towards JavaScript

+5
source share

I think the answer is no, most languages ​​do not consider "undefined" to be false. Of course, it is important to know the features of the language in order to understand how it handles true , false , NULL (or nil ), etc.

You noted this with Ruby, so here are some examples of Ruby:

 >> x # raises NameError: undefined local variable >> x = nil # initializes x and assigns the value nil >> x == true # false >> x == false # false (in Ruby nil is not false, but see below) >> x.nil? # true >> x ? true : false # false -- Ruby treats nil values as false in conditions >> x = 1 # now x has a value >> x.nil? # false >> x ? true : false # true 

As for “why evaluate undefined as false”, this can be convenient when you need to know that a variable is defined before using it. For example, in Ruby, you often see this:

 if x && x == "some interesting value" do_important_thing end 

If x is undefined, the first part of the condition returns false and shorts the operator. Which is much cleaner than:

 if x.nil? if x == "some interesting value" do_important_thing end end 
+4
source share

I just came across this with Javascript, and when that happens, the value is actually a string defined as "undefined"

If you were to test this:

 if (someVar==true) { document.write('True'); } else if (someVar==false) { document.write('False'); } else { document.write('Other'); } 

It will print "Other" as opposed to true or false - I don't know if that helps ...

+2
source share

Not necessary!

It depends on what you mean by "undefined". If you mean “uninitialized,” then the answer is no.

In C or C ++, assume that there is an instruction

 int x; 

in the area of ​​functions. After the statement is executed, x is an integer integer variable, but is not initialized. x can be any value. If it takes the value 0 , then it is false ; for all other values ​​(the probability of which is much higher), this is true . However, in the end, it does not matter what value x actually gets. In terms of program correctness, this is "undefined behavior."

Although not provided by the language, it is also possible to implement a tri-state, for example. true, false, unknown. See, for example, tribool in the Boost tribool .

+1
source share

Depends on what you mean with "equal to false". PHP has two different operators. It can be said that value == false means that the value is a scalar that evaluates to false, and value === false is that it is undefined.

Similarly, SQL is NULL , which is a special value that is not associated with 0 (or FALSE).

C / C ++ does not really have the concept of "undefined", only "uninitialized", in which case the variable exists, but its value is undefined and should not be read if you want your code to not suck.

In a scalar sense (that is, if you have something that contains a value), false is often defined as "0" and true as "non-zero", but this is not the standard across the board.

+1
source share

All Articles