1

I can't understand why the result of code execution gonna be -1 (in strict mode only):

'use strict'
[0,1].indexOf(0)

Is it bug or there is some other reason? Note: in normal mode it works as expected: index of 0 is 0

I try to analyze Polyfills code on mdn and have just one idea why it can happen: in strict mode in some cases this is not refer to window, but to 'undefined'.

epascarello
  • 195,511
  • 20
  • 184
  • 225
wwwebman
  • 879
  • 11
  • 15

2 Answers2

4

It is more like how the engine is evaluating your code.

The browser is seeing it as one line, not two

'use strict'[0,1].indexOf(0)

which evaluates to

's'.indexOf(0)

which is -1

Now if you ran that same code with a semicolon, you would get 0

'use strict';
[0,1].indexOf(0)
epascarello
  • 195,511
  • 20
  • 184
  • 225
  • Correct, you could add that he has to add a semicolon after `'use strict'` to make it to work. Edit: see you have already added that. – GuyT Mar 23 '18 at 14:01
1

ASI kicks you.

'use strict'; // NB
[0,1].indexOf(0)
Yury Tarabanko
  • 42,049
  • 9
  • 81
  • 96