JavaScript Why is this Array.sort behaviour different in Chrome vs Node.js

Problem

Let's make a basic list and sort it to make sure that 2 is ALWAYS first in the list. Simple enough, right?

[1, 2, 3].sort((a, b) => {
  if (a === 2) return -1;
  return 0;    
});

Chrome result: ✓

[2, 1, 3]

Node result: X

[1, 2, 3]

In order to get this behaviour in Node, you could - weirdly enough - look at the b parameter and make it return 1 if it's 2:

[1, 2, 3].sort((a, b) => {
  if (b === 2) return 1;
  return 0;    
});

With this implementation you get the opposite result; Chrome will be [1, 2, 3] and Node will be [2, 1, 3].

Questions

Do you have a logical explaination for this behaviour? Is my sorting function conceptually flawed? If so, how would you write this sorting behaviour?

Answer:1

I need to handle multiple event which will generate same HTML dynamically. I have added addEventListener for all elements. Also getting different event value. Now i just need to set this result to ...

I need to handle multiple event which will generate same HTML dynamically. I have added addEventListener for all elements. Also getting different event value. Now i just need to set this result to ...

  1. javascript multiple event listeners
  2. javascript multiple event handlers
  3. javascript multiple event listeners for the same event
  4. javascript multiple event listeners order
  5. javascript multiple event listeners same function
  6. javascript multiple event
  7. javascript multiple event handlers onclick
  8. javascript multiple event binding
  9. javascript event multiple functions
  10. javascript event multiple elements
  11. javascript event multiple keys
  12. javascript event multiple buttons
  13. javascript add multiple event listeners
  14. javascript prevent multiple event listeners
  15. javascript attach multiple event handlers
  16. javascript adding multiple event listeners
  17. javascript register multiple event handlers
  18. javascript remove multiple event listeners
  19. javascript add multiple event handlers
  20. javascript bind multiple event handlers

Following scenario/my solution consists of the following:  Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries ...

Following scenario/my solution consists of the following:  Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries ...

I'm trying to solve the task, get quantity of missed elements from an array. For example, if given an array [1,3,6], the quantity of missed elements is 3 (2,4,5). But somewhere code goes wrong and ...

I'm trying to solve the task, get quantity of missed elements from an array. For example, if given an array [1,3,6], the quantity of missed elements is 3 (2,4,5). But somewhere code goes wrong and ...

I make a node js web app for generating report. My idea is to use .jasper file (jasper report) to generate these reports. I've tried a bunch of node js library to do this, but nothing seems to work. ...

I make a node js web app for generating report. My idea is to use .jasper file (jasper report) to generate these reports. I've tried a bunch of node js library to do this, but nothing seems to work. ...

  1. file with node
  2. file upload with node js
  3. download file with node
  4. run file with node
  5. read file with node
  6. create file with node
  7. download file with node js
  8. read file with node js
  9. create file with node js
  10. open file with node.js
  11. delete file with node js
  12. write file with node js
  13. write file with node
  14. delete file with node
  15. upload file with node
  16. file upload with node and react
  17. open file with node
  18. file server with node.js
  19. node dockerfile
  20. send file with node js