-1

I created a function in Javascript but something should be wrong cause i haven't the awaiting result. The idea is to test several variable. In case they are null i would replace with a blanck space. Here is my code :

function chkNull(myObject) { 
   if (myObject == null){
       myObject = " ";
   }
}

And below i test with this :

var dOb = null;
chkNull(dOb);

I don't know what is wrong. If i test with if dOb == null below the var declaration, it is working.

Chris
  • 927
  • 1
  • 8
  • 13
  • 1
    Reassigning the value of the *function parameter* does not change the passed in argument. – VLAZ Dec 31 '19 at 16:49
  • I'm sorry, but this is in no way a duplicate question. Just because another question is relevant, even if it's the cause of the problem, doesn't mean they're both doing the same thing. – Brandon Dyer Dec 31 '19 at 17:08

1 Answers1

2

You need to return a value

function checkNull(value) {
    if (value == null) {
        return " ";
    } else {
        return value;
    }
}

And using it:

let notNull = checkNull(maybeNull);

With your example:

let dOb = null;
dob = checkNull(dOb);
Brandon Dyer
  • 1,316
  • 12
  • 21