0

Let's say there is an

<input id="exist" type="text">

and its value is 'something is in here'.

So in the console, I typed several codes.. to check 'input.value' is 'true'...

These are the codes..

var a = document.getElementById='exist';
a.value;         // result : 'something is in here'
a.value == 1;    // result : false
a.value == true; // result : false

But when input.exist was has empty value, results were like below.

a.value; // result : ''
a.value == 1;      // result : false
a.value == true;   // result : false
a.value == 0;      // result : true
a.value == false;  // result : true

Why this happening? How to check about elem.input.value is true?

goJson
  • 203
  • 2
  • 6
  • 12
  • If you need this in context of flow control, `if (a.value)` will do. – Michael Berkowski Sep 18 '14 at 17:15
  • 2
    http://stackoverflow.com/questions/359494/does-it-matter-which-equals-operator-vs-i-use-in-javascript-comparisons – dee-see Sep 18 '14 at 17:16
  • What do you want from true? Are you expecting it to return true if it has a value? `a.value != ""` would work or `if(a.value){...}`. `a.value == 1` is false because it turns 1 into `"1"` and matches the strings which is clearly not true. – scrappedcola Sep 18 '14 at 17:17
  • If you're somewhat new to javascript, and even if you aren't, a good in-depth and easy to understand resource is http://eloquentjavascript.net/ directly related to this question is the section on "Automatic Type Conversion" in the section "Values, Types, and Operators" – SnowInferno Sep 18 '14 at 17:25
  • `input.value` returns the value of the input. It will never be `'true'` unless you type "true" in the input. Is that what you are trying to do? The question is unclear. – Felix Kling Sep 18 '14 at 17:47

3 Answers3

1

The reason '' == 0 is because in Javascript, == will use type coercion to attempt to equate the values. If you want to check to see if the values are equal without type coercion, use ===.

A simple way to check for a value being present is !!a.value (!a.value will be true when the value is empty, negated again makes it false)

SnowInferno
  • 449
  • 3
  • 9
  • 1
    Just FYI, the `!!` in `!!a.value` isn't necessary when used in comparisons. It's only useful if you want to know the truthyness **boolean value**. For example, you can just do `if (a.value)` or `a.value ? something : somethingElse` and the same thing is accomplished. Other than if something needs to know `true`/`false` specifically, can you explain a use for `!!`? – Ian Sep 18 '14 at 17:27
  • You have a point, both of these accomplish the same goal. I find it to be a little more clear that you're checking to see if there is a value to use !!a.value, particularly if the code is going to be read by someone who is coming to JS from another language that does not equate null or undefined to a boolean value. Even when I do use constructs similar to if( a.value ), I tend to explicitly check for if( a.value !== null && a.value !== undefined ) for readability and comprehension. – SnowInferno Sep 18 '14 at 17:34
  • Those are great points, I just wanted to hear your opinion. I agree though, I tend to do explicit comparisons like you, because I expect the types to be known. But even still, there's something about empty strings that causes me to use `if (stringValue)` or `if (!stringValue)` and I wish I didn't. I'd rather be consistent and clear, but I can't help it for this! – Ian Sep 18 '14 at 17:38
0

the a.value will be always a String. So if you want to check if the value is exactly "true" then you should use a.value === "true".

Peruggia
  • 191
  • 4
-1

When you make the comparisson:

a.value == 0;      // result : true
a.value == false;  // result : true

It returns true because false or 0 means it is empty. Like when you check if there is something written in it

if (a.value) // This will return true if something is written in it. (not empty)

Try casting it to string to do the comparisson:

a.value == String(0); // result : false
a.value == String(false); // result : false
Carlos Calla
  • 6,556
  • 2
  • 16
  • 23
  • 1
    While this is a valid answer, it is not what is being asked for. The question is looking for simple presence of a value, not a particular value. – SnowInferno Sep 18 '14 at 17:26
  • After reading your response again more carefully, I see where I went wrong in understanding your answer. The part about casting to string for the comparison threw me off. You have my apologies. – SnowInferno Sep 18 '14 at 17:44
  • :O its ok, I got two downvotes though. My answer is solid but I guess people are not getting it. I hope the OP gets the idea. – Carlos Calla Sep 18 '14 at 17:45
  • Actually `a.value == 0;` will also be true if the value is `" "`, i.e. white spaces (not empty). Anyway, I have no idea what the OP is really trying to do. – Felix Kling Sep 18 '14 at 17:48
  • One of those downvotes was mine, I tried to take it back but since it was past 15 minutes it's locked in unless the answer gets edited :( – SnowInferno Sep 18 '14 at 17:51
  • @SnowInferno I edited just in case you want to take it back. I may delete the answer some time after but I will keep it there for a while because It may be useful to the OP. – Carlos Calla Sep 18 '14 at 18:52