0

code like this

<script type="text/javascript">
  var name = [2,3,4];
  var a = (1 in name);
  console.log(a);
</script>

but throw error

enter image description here

if code like this:

<script type="text/javascript">
  var names = [2,3,4];
  var a = (1 in names);
  console.log(a);
</script>

no error! why? test on firefox!

1 Answers1

-1

Since there is a possibility of scope issues with the variable name, do create a scope with something like this:

(function() {
  var name = [2,3,4];
  var a = (1 in name);
  console.log(a);
})();
Candide
  • 30,469
  • 8
  • 53
  • 60