开发者

Is it common for a language to evalute undefined as equal to false? If so, why is this done?

UPDATE: Question still unanswered. @Alastair_Pitts: Unless I'm missing something, it's a two part question. The开发者_如何学JAVA second part, "If so, why is this done?" and not been answered.


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

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


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


In many, if not most, languages values are either falsy, meaning that something doesn't exist or lacks value, or truthy, meaning that something exists or has value. The list of falsy values is usually: (these evaluate to false)

  1. 0 (zero, the number)
  2. '' (an empty string)
  3. null (if this value exists)
  4. undefined (if this value exists)
  5. false/False (if it has a boolean type)

Anything else is truthy and evaluates to true.

Edit: Made the answer a bit less biased towards JavaScript


I think the answer is no, most languages do not consider "undefined" to be the same as false. Of course it's important to know the particulars of a language to understand how it handles true, false, NULL (or nil), etc.

You tagged this with Ruby, so here are some Ruby examples:

>> 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 to "why evaluate undefined as false", it can be handy when you need to know a variable is defined before you use 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 the statement short-circuits. Which is much cleaner than:

if x.nil?
  if x == "some interesting value"
    do_important_thing
  end
end


I have only come across this with Javascript and when this occurs, the value is actually a string defined as 'undefined'

If you were to run a test on this:

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

It will output 'Other' as opposed to true or false - don't know if this helps...


Depends what you mean with "equal to false". PHP specifically has two different operators. You could say value == false meaning that value is a scalar that evaluates to false, and value === false in that it is undefined.

Likewise, SQL has NULL, which is a special value that is not related to 0 (or FALSE).

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

In a scalar sense (i.e. IF you have something that holds a value), false is often defined as "0", and true as "non-zero", but that's not a standard across the board.


Not necessarily!

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

In C or C++, assume there is a statement

int x;

in function scope. After the statement is executed, x is a legal integer variable, but it is not initialized. x can take any value. If it takes value 0, then it is false; for all other values (whose probability is much higher), it is true. However, at the end, it does not matter what value x actually acquires. From the program correctness standpoint, the it is "undefined behavior."

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜