-1

I'm trying to create a personalized welcome message on the homepage, that basically says "welcome [username]" providing the user has registered and their username goes into localStorage. That works fine, but I only want the message to display the username if the user has actually registered, (otherwise it says "welcome Null". So I'm trying to use the following code to do that:

// Stores username inputted on registration page inside Local Storage//
function storeUsrName() {
   var regName= document.getElementById("regName");
   localStorage.setItem("usrName", regName.value);
   alert("Thanks for registering!");
 };

// Display welcome message //
$(document).ready(function() {
  var usrName = localStorage.getItem("usrName");

  if (usrName !="null" || usrName !="undefined") {
    document.getElementById("welcomeMessage").innerHTML = ("Welcome " + usrName + "! ");
  } else {
    document.getElementById("welcomeMessage").innerHTML = ("Welcome! ");
  }
});

If a user registers and usrName is assigned a value, the welcome message works fine. But I expect the welcome message to say "Welcome!" if its the users first time visiting the site, and they have yet to register, and usrName has no value (null, or undefined).

Instead, if usrName has no value, the if/else statement doesn't work correctly, and the welcome message displays "welcome null!", despite the fact that I only want usrName to be displayed in the welcome message IF the value is NOT null or undefined.

Is there some correct way of doing this?

Palmer
  • 87
  • 1
  • 1
  • 9
  • Took no more than typing "javascript compare null undefined" into Google to find the duplicate ... please make an actual effort, thank you. – CBroe Dec 09 '17 at 07:40

3 Answers3

2

usrName !="null" is comparing with string null, change if (usrName !="null" || usrName !="undefined") { to

if (usrName !== null || usrName !==undefined) {.. rest of code}
brk
  • 48,835
  • 10
  • 56
  • 78
  • this still returned "welcome null!' for me – Palmer Dec 09 '17 at 07:45
  • The underlying logic is wrong. If `usrName === null` then `usrName !== undefined` and the personalized greeting still shows. The condition should be `(usrName !== null && usrName !== undefined)` or `(usrName === null || usrName === undefined)`, or, preferably, `(usrName)`. – AuxTaco Dec 09 '17 at 10:56
  • I am comparing someVariable != "null" on my machine. Apparently it is making null a string. So it really just depends on how the information is being retrived from your database. – William Howley Apr 15 '20 at 16:55
1

null and undefined are constant values, not strings. You need to omit the "" part from them. Also you can combine the null and undefined checking inside a single check like this

if(userName) {
   // ...your code
} 

Because null and undefined are falsy values, you can try to evaluate userName in the if statement without checking for each case.

Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

May be null is not string so that "null" this is not valid for null you can also write like following

usrName != "".(care that only double quotes no space between them!) I haven't idea for undefined sorry for this.

K_D
  • 1
  • 2