Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?

what is javascript
what is javascript used for in web design
advantages of javascript
what does javascript do
learn javascript
javascript help
javascript website
javascript definition

Why does the "in" operator in Javascript return true when testing if "0" exists in array, even when the array doesn't appear to contain "0"?

For example, this returns true, and makes sense:

var x = [1,2];
1 in x; // true

This returns false, and makes sense:

var x = [1,2];
3 in x; // false

However this returns true, and I don't understand why:

var x = [1,2];
0 in x;

It refers to the index or key, not the value. 0 and 1 are the valid indices for that array. There are also valid keys, including "length" and "toSource". Try 2 in x. That will be false (since JavaScript arrays are 0-indexed).

See the MDN documentation.

What do you need javascript for?, While JavaScript is not the only client-side scripting language on the Internet, it was one of the first and it is still the most widely used. js makes responsive design easier. JavaScript has become integral to the Internet experience as developers build increased interaction and complexity into their applications. JavaScript allows you to create highly responsive interfaces that improve the user experience and provide dynamic functionality, without having to wait for the server to react and show another page.

The in operator doesn't do what you're thinking it does. The in operator returns true if the specified operand is a property of the object. For arrays, it returns true if the operand is a valid index (which makes sense if think of arrays as a special-case object where the properties are simply named 0, 1, 2, ...)

For example, try this:

javascript:var x=[1,4,6]; alert(2 in x);

It'll also return true, because "2" is a valid index into the array. In the same way, "0" is an index into the array, so also returns true.

An Introduction to JavaScript, This chapter looks at seven aspects that are important when you are choosing a programming language and argues that JavaScript does well overall: Is it freely  JavaScript has become an essential web technology along with HTML and CSS, as most browsers implement JavaScript. Thus, You must learn JavaScript if you want to get into web development, and you must learn it well if you're planning on being a front-end developer or on using JavaScript for backend development.

Javascript's in operator does not check if a value is contained in an array. It checks if the object has a property or index. So var x = [4,5]; 4 in x; //false 1 in x; //true.

Because length is a property of x, "length" in x; //true

JavaScript is the Programming Language for the Web. JavaScript can update and change both HTML and CSS. In this example, x, y, and z, are variables:  JavaScript is a dynamically typed, prototype-based programming language that honors the ECMAScript Standard, thus supporting imperative, object-oriented and functional programming paradigms.

Modern browsers, except IE, support a couple methods that can find a value in an array.

indexOf and lastIndexOf return the first(or last) index of an exact match of their argument in an array, or -1, if no matching element was found.

if(A.indexOf(0)!= -1){
    // the array contains an element with the value 0.

You can add one or both methods to IE and older browsers-

    Array.prototype.indexOf= function(what, i){
        i= i || 0;
        var L= this.length;
        while(i< L){
            if(this[i]=== what) return i;
        return -1;
    Array.prototype.lastIndexOf= function(what, i){
        var L= this.length;
        i= i || L-1;
        if(isNaN(i) || i>= L) i= L-1;
        else if(i< 0) i += L;
        while(i> -1){
            if(this[i]=== what) return i;
        return -1;

In most cases, JavaScript is used to create responsive, interactive elements for web pages, enhancing the user experience. Things like menus,  JavaScript is technically a "dialect" of ECMAScript, the Mozilla Foundation can use "JavaScript" as the name of their implementations (currently present on the Rhino and SpiderMonkey engines). In the early days, Microsoft decided also to do what Netscape was doing on their own browser, and they developed JScript , which is also an ECMAScript

I guess you use Python before, in JS, use Array.prototype.includes

let x = [1, 2]
x.includes(1) // true

in operator check the indices of the array not the value

0 in [1, 2] // true
2 in [1, 2] // false

JavaScript was initially created to “make web pages alive”. The programs in this language are called scripts. They can be  Nowadays almost all web pages contain JavaScript, a scripting programming language that runs on visitor's web browser. It makes web pages functional for specific purposes and if disabled for some reason, the content or the functionality of the web page can be limited or unavailable.

JavaScript is a scripting language used to create and control dynamic website content, i.e. anything that moves, refreshes, or otherwise changes on your screen​  Updated July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScriptidentifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

JavaScript is a text-based programming language used both on the client-side and server-side that allows you to make web pages interactive. It doesn't sound the OP is asking why people think JS sucks as much as making a rhetorical statement to just advertise this flatassembler app. There's nothing wrong with promoting something you made, but it's not clear to anyone if the purpose of this post (tagged with "help") is asking about js as a language, our thoughts on flatassembler, or

While JavaScript's quirks aren't nearly as bad as PHP's, they are still annoyances and will make your life miserable. It's another example of a 

  • Thank you providing a workaround to the problem rather than just an explanation as to why it doesn't work.