You're viewing the legacy docs. They are deprecated as of May 18, 2016.
These docs are for version 2.4.2 and below of the Javascript SDK. Go to our current docs, or see our Web migration guide.

Version 2.4.2Changelog

DataSnapshot.key()

The last token in a Firebase database location is considered its key. For example, "fred" is the key for the /users/fred/ node. Calling key() on any DataSnapshot will return the key for the location that generated it. However, calling key() on the root URL of a database will return null.

var fredRef = new Firebase("https://docs-examples.firebaseio.com/samplechat/users/fred");
fredRef.on("value", function(fredSnapshot) {
  var key = fredSnapshot.key();
  // key === "fred"

  var childKey = fredSnapshot.child("name/last").key();
  // childKey === "last"
});
var rootRef = new Firebase("https://docs-examples.firebaseio.com");
rootRef.on("value", function(rootSnapshot) {
  var key = rootSnapshot.key();
  // key === null

  var childKey = rootSnapshot.child("samplechat/users/fred").key();
  // childKey === "fred"
});