How to compare old and new value with Cloud Functions for Firebase with .onWrite or onchange?

UPDATE: Cloud Functions recently introduced changes to the API as noted here.

Now (>= v1.0.0)

exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
  const beforeData = change.before.val(); // data before the write
  const afterData = change.after.val(); // data after the write
});

Before (<= v0.9.1)

exports.dbWrite = functions.database.ref('/path').onWrite((event) => {
  const beforeData = event.data.previous.val(); // data before the write
  const afterData = event.data.val(); // data after the write
});

Now that these functions are deprecated and this is the number one search result for this subject, here is the new updated version that Cloud Functions now use.

exports.yourFunction = functions.database.ref('/path/{randomPath}/goes/here').onWrite((change, context) => {
    // Your value after the write
    const newVal = change.after.val();
    // Your value before the write
    const oldVal = change.before.val();
});