I'm sure that Node won't evaluate the second part of the || in the if statement when the first part is true - but I fail to understand why the first part of the if statement is evaluating to false when username is a null object. Can someone help me understand what I've done wrong?

If he was getting the string "null", then he wouldn't be getting an error on "null".length" - it would be 4.
–
jmar777May 17 '12 at 12:40

@jmar777 It doesn't metter in this case, 'cause username === "null" is true in that case, and if's body will be executed.
–
EngineerMay 17 '12 at 12:43

Right, but the existing code is comparing to null, not "null" - so the error when calling .length is separate. That was a good catch on the funky String(data.match()) thing though.
–
jmar777May 17 '12 at 12:46

@jmar777 So OP's problem was comparing to null,but not to 'null'. Also I have updated my condition.
–
EngineerMay 17 '12 at 12:50

I don't think that was the case. The OP's original error was TypeError: Property 'length' of object null is not a function. If username at that point was actually a string containing "null", then that error would not happen, as username.length would return 4, rather than throw an error.
–
jmar777May 17 '12 at 12:59

Either username is no string then or you are doing something wrong!
–
AmberlampsMay 17 '12 at 12:47

I was wrong - length is properly evaluating to the length of the passed string. However, this doesn't solve the original problem of how do I know if I'm passed a null object? null.length doesn't evaluate?!
–
Amateur Programmer by NightMay 17 '12 at 12:52

If you require a non-empty string, you can do a simple "truthy" check that will work for null, undefined, '', etc:

if (username) { ... }

With that approach, you don't even need the .length check. Also, length is a property, not a method.

Edit: You have some funkiness going on. I think you need to start with how you're passing in your username - I don't think that your String(data.match(/\S+/)) logic is behaving the way that you're expecting it to (credit to @Engineer for spotting this).

Your match expression is going to return one or two types of values: null or an Array. In the case that it's null, as @Engineer pointed out, you end up passing in "null" as a string, which should resultantly pass your username check later on. You should consider revising this to:

This fixes the problem with the first part of the evaluation (thanks), but now username.length always evaluates to 1, no matter how long the data is coming in. Any ideas?
–
Amateur Programmer by NightMay 17 '12 at 12:38

I eventually want the length check to be more...it's a placeholder for more complex logic.
–
Amateur Programmer by NightMay 17 '12 at 12:47

What was happening in the first run throughs is I was thinking .match was returning a string - it was returning an array, as you mention - which was evaluating to length[1] (since I was entering one name). Further logic was applied and I got lost in all the changes. All is sorted out now that I understand that String(null) gives you "null" and data.match() gives you an array. Thanks.
–
Amateur Programmer by NightMay 17 '12 at 13:13