49

Can someone tell me since which ECMA version the IN operator is available and which browsers (versions) support it ?

Explanation:

The IN-operator can be used like the following:

var myObject = {
    Firstname: 'Foo',
    Lastname: 'Bar'
};

if('Lastname' in myObject){
    // Lastname is an attribute of myObject
}
Shog9
  • 156,901
  • 35
  • 231
  • 235
jAndy
  • 231,737
  • 57
  • 305
  • 359

2 Answers2

56

It is defined in ECMAScript 3rd edition. It is available in IE 5.5+ and all in-use versions of Firefox, Chrome, Opera and Safari.

You can use it safe in the knowledge that it will work.

You should err on the side of caution when using it to check event support. All implementations except older Firefox versions support "eventname" in element as a test for DOM events.

"onclick" in document.body; // -> false in old Fx, true in others
document.body.setAttribute("onclick", "");
typeof(document.body.onclick == "function"); // -> true in Fx
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • 2
    It's only in IE 5.5+; IE 5.0 was the ‘problem browser’ for `in`, though obviously that worry is long gone now. – bobince May 27 '10 at 12:01
  • 1
    FWIW I couldn't see it in the 2nd ed @ http://www.ecma-international.org/publications/standards/Ecma-262-arch.htm – Alex K. May 27 '10 at 12:02
  • 1
    @Nick: it certainly is in 3.0, even though IE didn't support it at the time of publication. ECMAScript 3.1 was the development name for what became the Fifth Edition. – bobince May 27 '10 at 12:03
  • 1
    @bobince - You're indeed right, I had the *2nd* edition, not the 3rd up when comparing and couldn't find section 11.8.7, it is in 3.0. – Nick Craver May 27 '10 at 12:11
  • @bobince: When I put IE5, I was going by the MSDN documentation for the [in operator](http://msdn.microsoft.com/en-us/library/9k25hbz2(v=VS.85).aspx) which states that it was in JScript version 1, which was implemented in IE 3.0. I put 5 as a safer bet because IE3 isn't widely used. I assume the docs are wrong? They have been in the past *once or twice* :-) – Andy E May 27 '10 at 12:12
  • 2
    @Andy: you're right, that doc is indeed nonsense! I've got IE5/Win98 here and `in` definitely doesn't work. – bobince May 27 '10 at 13:37
  • @bobince: thought as much, not the first time I've been misled by MSDN :-) – Andy E May 27 '10 at 13:40
2

According to MDC, it's implemented in JavaScript 1.4.

According to Wikipedia:

  • Netscape Navigator 6.0
  • Firefox 1.0+
  • IE 5.5+
  • Opera 6.0+
  • Safari 3.0+
  • Chrome 1.0+

So I think you're probably OK :)

Skilldrick
  • 69,215
  • 34
  • 177
  • 229
  • because I can do `undefined = 'defined!';` earlier in the code, which is *obviously* beneficial. – Eric May 27 '10 at 11:53
  • 1
    Really, you should have put that as another answer, as now our comments make no sense. – Eric May 27 '10 at 11:57
  • @Skilldrick you can strikeout text in an answer using the `` tag apparently http://meta.stackexchange.com/questions/63768/does-markdown-have-a-way-to-express-strikeout – Adriano Oct 14 '14 at 10:50
  • note: I doubt Wikipedia is a reliable source regarding Javascript features browser versions support. – Adriano Oct 15 '14 at 07:49