-1

I am trying to check below condition,

var param= document.getElementById("txtbox").value;
if(param!== null  || param.length!==0 || param !=="" || param!==undefined)
{//do something}

but even if param is null it is entering in the if block. Does anyone have an idea about this?

Dimitri Kopriwa
  • 13,139
  • 27
  • 98
  • 204
Faran Saleem
  • 404
  • 1
  • 7
  • 31
  • Just check `param !== ''` only, the other checks are meaningless. – Teemu Feb 10 '20 at 12:14
  • Even checking that.. it still enters if block.. But it should not – Faran Saleem Feb 10 '20 at 12:15
  • 1
    What element is `#txtbox`? – Teemu Feb 10 '20 at 12:18
  • It shouldn't be possible for `document.getElementById("txtbox").value` to be `null`. I can't think of any situation where it wouldn't be `undefined` (because the element doesn't have a `value` property at all) or a string. You need to provide a [mcve]. – Quentin Feb 10 '20 at 12:23
  • @Quentin Maybe `value` is a custom property, and set to `null` ..? – Teemu Feb 10 '20 at 12:25
  • 1
    @Teemu — Custom property maybe, but not a custom attribute (which would be invalid HTML and generate a string). An [mcve] would answer that though. – Quentin Feb 10 '20 at 12:26
  • @FaranSaleem If `#txtbox` is an empty input element or a textarea element, its value is the empty string. In that case you should make a strict comparison to the empty string only, checking other alternatives with logical OR will make the check useless, because some of them will pass in all cases, nevertheless the value of `param`. – Teemu Feb 10 '20 at 12:41

4 Answers4

0

What about this:

var param= document.getElementById("txtbox");
if(!param && !param.value)
{
    //do something
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • dont you want the oposite if (!!param && !!param.value) – EugenSunic Feb 10 '20 at 12:16
  • `!param` passes if the element does not exist. Then it tries to check the value of som falsy value, which will trigger an error. – Teemu Feb 10 '20 at 12:16
  • 1
    @faransaleem : agree with salahakbari, the order of your tests is really strange, you can't test first param.length!==0 and THEN param !=="". First check if param exists, then check its length property (logic here) – Pierre Feb 10 '20 at 12:16
0

You can null check element like,

function checkNull() {
    var param= document.getElementById("txtbox").value;
    if(!param)
      console.log("Null check passed")    
    else
      console.log(param+" Entered by user")
  }
<input type="text" id="txtbox" name="txtbox">
<button onclick="checkNull()">Null checker</button>
Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
0

unfortunely your code's logic is incorrect.

For example if param is null, param!=="" is true. Because null is different "" (empty string) and etc.

You may check only param variable.

For example:

var param= document.getElementById("txtbox").value;
if(param)
{//do something}

But also document.getElementById("txtbox") may be null or undefined, therefore you must check it.

For example:

var txtBox= document.getElementById("txtbox");
    if(txtBox&&txtBox.value)
    {//do something}
Ramil Aliyev 007
  • 4,437
  • 2
  • 31
  • 47
-1

document.getElementById will return you single object that is representing first element with wanted ID, and it will return null if there is no element founded sou:

if(!param) {}

should be enough