Perform action on different server responses

On the moment, I am at the point where the client can send requests to the server, have the server perform actions and send back a response to the client.

For exemple, when I want to change a password, I verify the old password in my database. If it matches, the password is updated and I send a 200 response like so :

sendResponse("User's Password Updated", exchange, 200, true);
which is read in chrome’s network tab as : { "response" : "User's Password Updated"}

If the old password is wrong (can’t verify the user’s identity), it sends the following message : sendResponse("User's password could not be verified", exchange, 200, true);
which is read in chrome’s network tab as : { "response" : "User's password could not be verified"}

Now what I want to do is to perform a different action on my client for each action. For this exemple, let’s log a message.

I want to have something like that (pseudocode):

if (response === "User's password could not be verified")
   console.log('User's password could not be verified');
else if ( response === "User's Password Updated")
   console.log('User's Password Updated');

keep in mind this is my first time using http, and I am pretty happy with myself being able to display the different responses in the network tab

Answer

As I said in my comment you should conditionally display a message based on the http response code, not message.

To do that you would modify your code to this:

sendResponse("User's password could not be verified", exchange, 401, true);

Then in your if statement you would do:

if (response.status === 401) {
   console.log('User's password could not be verified');
} else if ( response === 200) {
   console.log('User's Password Updated');
}

I suggest you read up on the response object: Response and HTTP Status Codes..

This also may help.