1

I've got a for..of javascript loop to loop through an array. My code snippet works fine in Chrome, IE & Firefox consoles without any problems but fails in the safari (version5.1.7) console. I get the following error:

line: 287
message: "Expected an identifier but found 'arr' instead"
sourceId: 2082538144
__proto__: Error

My code snippet is pretty simple and is as follows:

let arr = [11, 22, 34, 45, 66, 77, 88];

for(let elem of arr){
  console.log(elem);
  if(elem > 50){
    break;
  }
}

Has anyone else faced this issue and solved it?

31piy
  • 23,323
  • 6
  • 47
  • 67
Roger Dodger
  • 927
  • 2
  • 16
  • 37

1 Answers1

4

According to MDN, for...of is supported from Safari 8 onwards.

So that might explain why it doesn't work with your Safari 5.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • Update: I can't find a safari download for windows 10 that is higher than version 5 – Roger Dodger Dec 30 '17 at 08:32
  • 1
    @RogerDodger hmm...it might be a good idea to try [`array#forEach`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Browser_compatibility) then. – Nisarg Shah Dec 30 '17 at 08:43
  • My go to usually is the forEach loop construct - but 'break' doesn't work in forEach, which is why I tried for...of which can handle a break statement. – Roger Dodger Dec 30 '17 at 09:07
  • 1
    @RogerDodger Yes, that's one of the limitations with `forEach`, but [there are ways](https://stackoverflow.com/a/2641374/5894241) to work around that. Or, you can go with the `for` loop. – Nisarg Shah Dec 30 '17 at 09:13