Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for empty value in Javascript?

I am working on a method of retrieving an array of hidden inputs in my form like so

<input type="hidden" value="12:34:00" name="timetemp0">
<input type="hidden" value="14:45:00" name="timetemp1">
<input type="hidden" value="15:12:00" name="timetemp2">
<input type="hidden" value="16:42:12" name="timetemp3">
<input type="hidden" value="16:54:56" name="timetemp4">
<input type="hidden" value="17:03:10" name="timetemp5">

My javascript function retrieves these individually by using getElementsByName('timetemp'+i)

    for (i ; i < counter[0].value; i++)
        {
//finds hidden element by using concatenation of base name plus counter

var timetemp = document.getElementsByName('timetemp'+i);

//if there is a value alert that value to user - this is just for testing purposes at the moment
//because there is only one of timetemp.i then it occupies position 0 in array
            if (timetemp[0].value == null)
            {
                alert ('No value');

            }
            else
            {
                alert (timetemp[0].value);

            }
}

So what should happen is it will alert the user of the value in that hidden input but if it comes accross an input with no value like this:

<input type="hidden" value="" name="timetemp16">

Then it will say "No value"

However th if function cannot seem to work with this:

I have tried:

  1. (timetemp[0].value == null)
  2. (timetemp[0].value === null)
  3. (timetemp[0].value == undefined)
  4. (timetemp[0].value == '')

It always seems to default to else clause.

Any ideas?

like image 439
sqlmole Avatar asked Aug 09 '11 13:08

sqlmole


People also ask

How do you check the value is empty or not in JavaScript?

Answer: Use the === Operator You can use the strict equality operator ( === ) to check whether a string is empty or not.

How do you check if a value is empty?

The IS NULL operator is used to test for empty values (NULL values).

What is a blank value in JavaScript?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

Is empty string null in JavaScript?

The value null represents the absence of any object, while the empty string is an object of type String with zero characters. If you try to compare the two, they are not the same.


2 Answers

Comment as an answer:

if (timetime[0].value)

This works because any variable in JS can be evaluated as a boolean, so this will generally catch things that are empty, null, or undefined.

like image 168
Richard Dalton Avatar answered Sep 19 '22 01:09

Richard Dalton


In my opinion, using "if(value)" to judge a value whether is an empty value is not strict, because the result of "v?true:false" is false when the value of v is 0(0 is not an empty value). You can use this function:

const isEmptyValue = (value) => {
    if (value === '' || value === null || value === undefined) {
        return true
    } else {
        return false
    }
}
like image 43
Jamter Avatar answered Sep 18 '22 01:09

Jamter