> For the complete documentation index, see [llms.txt](https://sh20raj.gitbook.io/wtf/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sh20raj.gitbook.io/wtf/javascript/undefined.md).

# \[] == !\[]

JavaScript, the ubiquitous language of the web, is known for its quirks and peculiarities. One such enigma that often baffles both novice and experienced developers is the expression \[] == !\[]. This seemingly simple comparison results in true, leading many to exclaim, "WTF, JavaScript?!". Let's dive into the mechanics behind this puzzling behavior and uncover the logic (or lack thereof) that drives it.

#### See Video

{% embed url="<https://www.youtube.com/watch?v=XPf4KMWPVFo>" %}

####

#### Step-by-Step Explanation

1. **Starting with `![]`:**
   * `[]` is an empty array.
   * When you put `!` (which means "not") in front of something, it turns it into its opposite.
   * An empty array is kind of like saying "something exists" (so it's like `true`).
   * `![]` means "not an empty array," which is like saying "not true," so it becomes `false`.
2. **Now we have `[] == false`:**
   * So now our comparison is `[] == false`.
   * `==` is used to check if two things are equal.
3. **Type Coercion:**
   * JavaScript tries to make different things look the same when comparing them. This is called "type coercion."
   * When comparing an array (`[]`) and a boolean (`false`), JavaScript converts both to numbers.
4. **Converting `false` to a Number:**
   * `false` becomes `0`.
5. **Converting `[]` to a Number:**
   * An empty array (`[]`) becomes an empty string `""`.
   * An empty string `""` becomes `0`.
6. **Final Comparison:**
   * Now we have `0 == 0`, which is `true`.

#### Putting It All Together

* Imagine you have an empty box (`[]`).
* `![]` asks, "Is this box not empty?" Since it is empty, the answer is `false`.
* When you compare the empty box to `false`, JavaScript turns both into `0`.
* Since `0` is equal to `0`, the final result is `true`.

So, `[] == ![]` is `true` because JavaScript does some magic behind the scenes to turn the empty box and `false` into the same thing (both `0`), and then says they are equal.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sh20raj.gitbook.io/wtf/javascript/undefined.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
