Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript IN operator compatibility

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
}
like image 833
jAndy Avatar asked May 27 '10 11:05

jAndy


People also ask

Can I use JavaScript in operator?

The JavaScript in operator is used to check if a specified property exists in an object or in its inherited properties (in other words, its prototype chain). The in operator returns true if the specified property exists. Anatomy of a simple JavaScript object.

What is === operator in JavaScript?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

Can you use in operator on array JavaScript?

JavaScript in operator to check whether the Array is using a key or the index for its values. JavaScript's “in” operator can also work with arrays as well.

What does 3 dots mean in JavaScript?

(three dots in JavaScript) is called the Spread Syntax or Spread Operator. This allows an iterable such as an array expression or string to be expanded or an object expression to be expanded wherever placed.


2 Answers

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
like image 182
Andy E Avatar answered Oct 06 '22 02:10

Andy E


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 :)

like image 42
Skilldrick Avatar answered Oct 06 '22 00:10

Skilldrick