0

I'm using Node.js, I've made a function that looks like this:

function makeTitleWith(name) {
   if (name === null) {
         return "Hello, welcome to my app!"
   } else {
         return "Hello, welcome " + name + "!"
   }
}

And when I call it with a variable thats null, it returns "Hello, welcome null!". I'm new pretty new to JS, and this is driving me insane. I've also tried == instead of === but it makes no difference.

4
  • 1
    @T.J.Crowder Sorry, it executes the other block. Not the === null one. I fixed my question now. Commented Jul 23, 2019 at 8:30
  • Then please add a minimal reproducible example that shows the actual problem. Commented Jul 23, 2019 at 8:32
  • @andymccullough Yes I have also tried checking for undefined, but it didnt change anything. And if the value im passing to the function would be undefined, it would log to the console as undefined instead of null right? Commented Jul 23, 2019 at 8:32
  • Can you please post here what the console.log(name) returns? Commented Jul 23, 2019 at 8:43

2 Answers 2

2

when I call it with a variable thats null, it returns "Hello, welcome null!"

That means you're not calling it with name set to null, you're calling it with name set to "null" (or an unlikely second possibility I'll cover later). "null" is not equal to null (either == or ===).

Example:

function makeTitleWith(name) {
   if (name === null) {
         return "Hello, welcome to my app!"
   } else {
         return "Hello, welcome " + name + "!"
   }
}
console.log(makeTitleWith("null"));
// or more likely:
var n = String(null);
console.log(makeTitleWith(n));

You probably want to fix where you're calling it, since that "null" is probably the result of converting null to string.


The unlikely second possibility: You're calling it with name set to an object that, when converted to string, converts to "null", like this:

function makeTitleWith(name) {
   if (name === null) {
         return "Hello, welcome to my app!"
   } else {
         return "Hello, welcome " + name + "!"
   }
}

var n = {
    toString() {
        return "null";
    }
};
console.log(makeTitleWith(n));

I very much doubt that's what's going on.

Sign up to request clarification or add additional context in comments.

6 Comments

"Hello, welcome " + name + "!" will coerce null into a string anyway.
@BenM - It would, but it won't given the OP's code, which checks for null and takes another branch.
This was the case, what would be the best way to prevent this from happening? Should i write my own toString function, which checks for the value being null and preventing it from being changed to string (and causing a disaster in my code), or is there some existing way that everyone uses?
@user109321948492842303 - Do the null check before converting to string, e.g. x = y === null ? null : String(y); or similar. You could make your own toNonNullString function or similar, but if you're doing your validation early, it isn't really needed.
As an aside, you can catch null, undefined, empty string, and false with: if(name)
|
0

I would recommend you can modify the above code like this (more like defensive coding) and expect/enforce the caller to send the string value as input param to the function.

if(typeof name == "string"){
    return "Hello, welcome " + name + "!";
}
return "Hello, welcome to my app!";

Also note, if the caller is calling the function with null or empty object, in both the cases, typeof operator returns object. So based on your case I don't think that the method was called with null or empty object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.