{"id":987,"date":"2009-08-11T20:20:05","date_gmt":"2009-08-12T01:20:05","guid":{"rendered":"http:\/\/unitstep.net\/?p=987"},"modified":"2009-08-11T20:20:05","modified_gmt":"2009-08-12T01:20:05","slug":"evaluation-of-boolean-values-in-javascript","status":"publish","type":"post","link":"https:\/\/unitstep.net\/blog\/2009\/08\/11\/evaluation-of-boolean-values-in-javascript\/","title":{"rendered":"Evaluation of boolean values in JavaScript"},"content":{"rendered":"
If you have a background in a strongly-typed language such as Java, you’ll be used to using logical operators only with boolean values\/expressions. However, in most dynamically-typed languages this doesn’t have to be the case, due to the nature of dynamic typing: The type of the variable is often determined based on the context<\/em> in which it is used.<\/p>\n With JavaScript<\/strong> there are actually two concepts at play when using logical operators: What is actually returned from the result of a logical operation, and how variables are converted to boolean values when the context requires it.<\/p>\n <\/p>\n Firstly, we’ll look at how variables in JavaScript are converted to boolean values. One way to explicitly convert a non-boolean value to a boolean one in JavaScript is to use the global Boolean object as a function<\/a>. By using the following code, you can explicitly get the boolean conversion of a variable or expression:<\/p>\n The variable So what values of This means that all other expressions or values, including any non-null object (including the Note that using the This is because the contract of the logical NOT operator in JavaScript is to return false if the operand can be converted to true and true otherwise. The second NOT simply reverses the negation done by the first NOT operator. While all of this may seem dead simple, it will be important to note as we move on to how other logical operators work.<\/p>\n This is perhaps the most important difference with JavaScript. Although the logical NOT operator (!) is guaranteed to return a boolean value, the logical AND (&&) and logical OR (||) operators are not<\/strong>. This is by design, and although it might be a bit of a change for some developers, the feature can actually be quite useful.<\/p>\n Consider the following code examples:<\/p>\n In this example of using logical AND, In this example of using logical OR, This means that one of the original operands or expressions used with the logical operators will be returned as a result of the evaluation. You will not always get an actual boolean value, unless both of the operands were booleans to begin with. Usually, this doesn’t matter, since if you use the result in a boolean context, it will get converted to the expected value. For example:<\/p>\n This example will output “At least one string was empty”, and then “a string that is not empty”. This is because the logical OR operator first converts This also highlights a convenient way of using logical OR – give me the first expression if it evaluates to true, otherwise give me the second. This is usually used to test for the availability of built-in objects in a particular JavaScript environment.<\/p>\n However, consider the following example:<\/p>\n In this slightly changed example, instead of directly supplying the result to the if-conditional, we test whether it is equal to This underscores an important point: If you actually want a boolean value to work with, you will have to explicitly convert it, using either the JavaScript offers some neat features due to its dynamic typing and these can help speed development, but you just need to be aware of how they work so that you don’t get tripped up. This is especially true when dealing with implicit type conversion and how logical operators work. I hope you found this helpful and as always, any feedback is appreciated!<\/p>\n If you have a background in a strongly-typed language such as Java, you’ll be used to using logical operators only with boolean values\/expressions. However, in most dynamically-typed languages this doesn’t have to be the case, due to the nature of dynamic typing: The type of the variable is often determined based on the context in […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[208,64,4,137],"tags":[361,451,405,362,438,263],"_links":{"self":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/987"}],"collection":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/comments?post=987"}],"version-history":[{"count":20,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/987\/revisions"}],"predecessor-version":[{"id":1007,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/posts\/987\/revisions\/1007"}],"wp:attachment":[{"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/media?parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/categories?post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unitstep.net\/wp-json\/wp\/v2\/tags?post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Undergoing a conversion<\/h2>\n
var asBoolean = Boolean(someVariable);<\/code><\/pre>\n
asBoolean<\/code> is now guaranteed to have a boolean value. Note that this is not the same as the expression
new Boolean(someVariable)<\/code>, as that returns a Boolean (wrapper) object representing the converted value of
someVariable<\/code>, not a boolean primitive.<\/strong><\/p>\n
someVariable<\/code> will result in
true<\/code> being returned, and which ones will result in
false?<\/code> The following values will evaluate to
false<\/code>, while all others will evaluate to
true<\/code>:<\/p>\n
\n
false<\/code> itself<\/li>\n<\/ul>\n
Boolean<\/code> object for false!) and the string “false” will be converted to true.<\/p>\n
Boolean<\/code> function isn’t the only way to explicitly convert a variable to its boolean equivalent. You could also apply the logical NOT operator twice, like so:<\/p>\n
var asBoolean = !(!someVariable);<\/code><\/pre>\n
Logical conversion<\/h2>\n
var result = a && b<\/code><\/pre>\n
a<\/code> is returned if it can be converted to false; otherwise
b<\/code> is returned.<\/strong><\/p>\n
var result = a || b<\/code><\/pre>\n
a<\/code> is returned if it can be converted to true; otherwise
b<\/code> is returned.<\/strong><\/p>\n
var string1 = \"\";\r\nvar string2 = \"a string that is not empty\";\r\nvar result = string1 || string2;\r\nif (result)\r\n{\r\n window.alert(\"At least one string was not empty\");\r\n window.alert(result);\r\n}<\/code><\/pre>\n
string1<\/code> to a boolean, which results in
false<\/code>. Thus, the result of the logical OR returns
string2<\/code> into the result. Since the result is now a non-empty string, it converts to
true<\/code> for the if-conditional. <\/p>\n
var string1 = \"\";\r\nvar string2 = \"a string that is not empty\";\r\nvar result = string1 || string2;\r\nif (true == result)\r\n{\r\n \/\/ We will never get here.\r\n window.alert(\"At least one string was not empty\");\r\n window.alert(result);\r\n}<\/code><\/pre>\n
true<\/code>. In this case, it fails, since when using the equality operator<\/a>, operands are not converted to booleans. (Using the strict equality operator also would not work)<\/p>\n
Boolean<\/code> function or a double application of the logical NOT operator<\/strong>, like so:<\/p>\n
result = Boolean(result);\r\n\/\/ Or, we could do this:\r\nresult = !(!result);<\/code><\/pre>\n
Conclusion<\/h2>\n
References<\/h3>\n
\n