-1
if(true){
    alert("hello");
}

if("string"){
    alert("hello");
}

The code above shows that a string can be used as a substitute for Boolean value "true". Why does this work and does this mean that Boolean value "true" and string have the same data type?

Jin
  • 1,902
  • 3
  • 15
  • 26
  • 2
    Guess what? `"false"` evaluates to `true` as well. See https://developer.mozilla.org/en-US/docs/Glossary/Truthy – haim770 Dec 26 '16 at 08:01
  • `"string"` does not evaluate to `false`. – guest271314 Dec 26 '16 at 08:02
  • just to be sure , You can use "string"==true or "string"===true in Your Condition . but console.log("string"==true) or console.log("string"===true) will return false just check it in your console of your browser – Alireza Masali Dec 26 '16 at 08:08

4 Answers4

1

A string as a statement, represents it's existence, in other words, if the string is set, it will return true, as if its "" it will return as false

This way you can check a variable if it is set or not

d3vi4nt
  • 130
  • 5
  • A string can't be `null`. A _variable_ may contain the `null` value, but if it has that value it isn't a string. However, the string value `""` is also falsy. See the MDN link in haim770's comment for details of truthy and falsy values. – Michael Geary Dec 26 '16 at 08:06
1

JavaScript's conditional operators such as if-else and a ? b : c don't require the condition to be of actual Boolean type but rather check whether it is truthy or a falsy value in JavaScript terms.

For example, such values as null and 0 are considered falsy so they would be treated like false in if-else and ternary operators.

In a broader sense this might be considered as a case of type coercion, see for example this question for more information. So when an interpreter sees non-boolean expression inside an if it is "re-writing" it like if (Boolean("string")) { ... }, i.e. invoking conversion from the expression to the exact boolean type.

Graham
  • 7,431
  • 18
  • 59
  • 84
Andrew Sklyarevsky
  • 2,095
  • 14
  • 17
0
if("string"){
    alert("hello");
}

since you did not include an operator, as default, it just checks for the existence of the string, "string" == true.

yuenhy
  • 41
  • 1
  • 8
  • It's not really checking for the _existence_ of the string. The string `""` exists but is falsy. – Michael Geary Dec 26 '16 at 08:09
  • oh, thanks for the point out. what would be a more appropriate answer to this question? – yuenhy Dec 26 '16 at 08:13
  • You're pretty close, perhaps if you read the MDN article that haim770's comment links to it will give some ideas on how to understand and answer the question better. – Michael Geary Dec 26 '16 at 08:16
0

JavaScript has a boolean type, with possible values true and false (both of which are keywords.) Any value can be converted to a boolean according to the following rules:
false, 0, empty strings (""), NaN, null, and undefined all become false. All other values become true.

You can perform this conversion explicitly using the Boolean() function:

Boolean(""); // false
Boolean(234); // true

https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript

Yichong
  • 707
  • 4
  • 10