0

var data = {
  name: 'home',
  href: '/'
};
var pathname = "/";
console.log(data.href == pathname == "/")

The console.log statement printed "false".

However, if I have the following:

var data = {name: 'home', href: '/'};
var pathname = "/";
console.log(data.href == "/");
console.log(data.href == pathname);
console.log(pathname == "/");

All three of the console.log statement will print true.

Phil
  • 157,677
  • 23
  • 242
  • 245
Rongeegee
  • 866
  • 3
  • 10
  • 30

2 Answers2

1

The comparison operator == is evaluated left to right. ref

data.href == pathname == "/"

becomes

true == "/"

which is false.

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
0
console.log(data.href == pathname == "/")

All three elements may be the same, but that's not what is being compared here.

Take the second and third elements. Both are /, so the result of the comparison is true. This is compared with the first element, but / is not true so the comparison fails.

The exact order of evaluation doesn't matter here. The first comparison returns a boolean, so the second comparison returns false.

mplungjan
  • 169,008
  • 28
  • 173
  • 236