Test if a data exist in Firebase

You can use DataSnapshot.hasChild to determine if a certain child exists.

usersRef.once('value', function(snapshot) {
  if (snapshot.hasChild(theDataToAdd)) {
    alert('exists');
  }
});

Here's a quick jsfiddle showing how this works: http://jsfiddle.net/PZ567/

But this will download all data under usersRef and perform the check on the client. It's much more efficient to only download the data for the user you want to check, by loading a more targeted ref:

usersRef.child(theDataToAdd).once('value', function(snapshot) {
  if (snapshot.exists()) {
    alert('exists');
  }
});

I use the following code:

var theDataToAdd = userName;
var ref = new Firebase('https://SampleChat.firebaseIO-demo.com/users/' + theDataToAdd);
ref.on('value', function(snapshot) {
   if (snapshot.exists())
      alert ("exist");
   else
      alert ("not exist");
});

My method is more lightweight than this:

usersRef.once('value', function(snapshot) {
  if (snapshot.hasChild(theDataToAdd)) {
    alert('exists');
  }
});

because a client will not fetch all users data, which can be huge.