VIEW THIS AS

Auto mode follows the Route Engine until you choose a viewpoint.

YOU ARE HERE

ROUTE CHECK

CONNECTED TO

WHAT NEXT

Use the canonical route for this room, or HELP if you are unsure.

The Gold Standard of Websites | Separating includes Membership from indexOf Results for NaN and Missing Slots

A catalogue contains an invalid numeric reading, yet one search says it is present and another reports no match. A sparse row also appears to contain undefined even though nobody assigned an undefined value to that position. These outcomes become understandable once we separate the comparison rule from the rule for visiting array positions.

The short answer is that ordinary array includes uses SameValueZero comparison and reads each searched position. Ordinary indexOf uses strict equality and checks whether a position exists before reading it. Consequently includes can find NaN and can treat an ordinary missing slot as undefined, while indexOf cannot find NaN and skips genuinely absent positions. Their return types also differ: one answers membership; the other returns a position or minus one.

This lesson follows original catalogue and measurement examples. It uses ordinary arrays with unmodified standard prototypes unless a section explicitly introduces inherited properties or accessors. No live customer data or production validator is involved. The aim is to choose an operation whose answer matches the question, then retain enough evidence to explain what the answer does and does not establish.

This specialist guide belongs to the Websites root. It also connects to The Gold Standard of Education | Learning That Transfers into Life and the How X Works hub. The worked cases are original teaching examples; their numbers are stipulated so that the reasoning can be checked.

eduKateSG · Websites

Find your next learning step

Compare includes and indexOf for NaN, missing slots and first positions. Work original ledgers, search boundaries, identity and practical catalogue checks.

ROUTE 1 · CHAPTERS 1–3

Choose the question

Separate membership, first position and the NaN comparison rule.

ROUTE 2 · CHAPTERS 4–6

Inspect values and presence

Work signed zero, object identity and missing slots versus explicit undefined.

ROUTE 3 · CHAPTERS 7–9

Trace the searched range

Follow a mixed ledger, starting boundaries and inherited properties.

ROUTE 4 · CHAPTERS 10–12

Check effects and limits

Observe early stopping and validate the actual data contract with bounded fixtures.

ROUTE 5 · CHAPTERS 13–14

Transfer to a catalogue

Preserve the visitor’s question and report comparison and source shape together.

Full chapter contents · Websites subject guide

Chapter contents

Choose the question · Chapters 1–3

1. Write the question before choosing the search

2. Establish the ordinary string and number baseline

3. Follow NaN through the comparison boundary

Inspect values and presence · Chapters 4–6

4. Keep signed zero and object identity in view

5. Describe an empty slot precisely

6. Compare undefined searches on sparse arrays

Trace the searched range · Chapters 7–9

7. Work a mixed six-position ledger

8. Treat fromIndex as a search boundary

9. Make inherited indexed values explicit

Check effects and limits · Chapters 10–12

10. Observe reads and early stopping without adding noise

11. Avoid turning membership into validation

12. Check original and changed-context fixtures

Transfer to a catalogue · Chapters 13–14

13. Transfer the reasoning to a catalogue filter

14. Report the comparison and the source shape together

CHAPTER 1 OF 14 · Choose the question

1. Write the question before choosing the search

Back to contents

Imagine a catalogue of three permitted display modes: compact, comfortable and detailed. The immediate question is whether comfortable occurs anywhere in that catalogue. A Boolean answer is enough for that particular decision. An includes call can express it directly, without asking the programmer to reinterpret a numeric position.

Another task asks where comfortable first occurs so that a selected tab can be highlighted. That task needs an index. An indexOf result supplies a position when a match exists, with minus one as the ordinary no-match result. The two operations may agree about a simple string while still answering different questions.

Write the input sequence and intended result type in the first ledger. For membership, record true or false. For a position, record the smallest matching index within the searched range or the no-match sentinel. This keeps a later conditional from silently treating a legitimate zero position as failure.

Now place compact first and ask for compact. Its first index is zero. An if statement that uses that numeric result directly treats zero as falsy. It can therefore enter the wrong branch even though the search succeeded. Conversely, minus one is truthy, so directly testing an unsuccessful indexOf result can reverse the intended decision.

The practical repair is to choose includes for an appropriate membership question or compare an index result explicitly with its documented no-match sentinel. Do not repair one example by adding a special case for compact. The underlying error concerns the result contract, not that particular catalogue label.

For transfer, put the requested mode last, then remove it entirely. Predict the numeric and Boolean results separately. Explain which result the website actually needs before choosing its condition. This first exercise isolates result interpretation before the more unusual comparison and missing-position cases appear.

Contents · Next chapter

CHAPTER 2 OF 14 · Choose the question

2. Establish the ordinary string and number baseline

Back to contents

Use a fresh array containing the numbers fourteen, twenty-eight and fourteen. Searching for fourteen with includes returns true. Searching with indexOf returns zero because the first matching entry is at the beginning. Searching for twenty-eight gives true and one respectively. These are unsurprising baseline cases against which changed conditions can be compared.

Search for the string fourteen rather than the number fourteen, using the actual spelling "14". Neither operation should be credited with automatically converting that string into the numeric entry in this ordinary example. Both comparisons distinguish the primitive types here. A developer who expects a form field's string to match a numeric catalogue needs an explicit input interpretation step.

That interpretation is a separate responsibility. Converting a supplied string to a number can itself accept or reject forms according to the chosen conversion rule. A successful membership result afterwards does not prove that the original text met the application's accepted input grammar. Keep raw input, conversion outcome and search outcome separate.

Duplicate values introduce another boundary. The array contains fourteen twice, but includes supplies no count and indexOf supplies only the first eligible position. Neither result establishes that exactly one matching record exists. If duplicate detection matters, use a deliberate counting or indexing process whose contract answers that additional question.

In the catalogue, two identical display labels might be harmless, while two records with the same supposed unique identifier might violate a requirement. The array search does not know which meaning applies. It compares values under its own rules and returns its own result; the website must supply the business interpretation.

For a changed-context check, use numbers fourteen and twenty-eight together with the string fourteen. Record which positions match a numeric query and which match the string query. Then ask separately whether there is a match, where the first match occurs and how many matches exist. Three distinct questions should produce three explicitly named results.

Contents · Previous chapter · Next chapter

CHAPTER 3 OF 14 · Choose the question

3. Follow NaN through the comparison boundary

Back to contents

NaN is a JavaScript numeric value used for results that do not represent an ordinary number. In this teaching fixture, place it deliberately between two valid measurements: twelve, NaN and thirty. We are not inferring how a real instrument produced it. We are investigating how a search treats a value already present in a known sequence.

An includes search for NaN returns true under its comparison rule. An indexOf search for NaN returns minus one even though the array contains that value. This is not evidence that one method failed to inspect the middle position or that the array lost the entry. The methods use different equality contracts.

Strict equality does not consider NaN equal to itself. The SameValueZero comparison used by includes does treat the NaN case as matching. Naming those rules explains the disagreement more accurately than saying one method is newer, smarter or more reliable. Each is implementing its stated contract.

Keep the invalid-reading classification separate from membership. Finding NaN does not repair the measurement, identify its cause or establish that every other reading is valid. It only answers whether an eligible compared value matches NaN under the selected rule. A data-quality check needs an explicit policy for all relevant invalid and missing states.

const readings = [12, NaN, 30];
readings.includes(NaN); // true
readings.indexOf(NaN); // -1

For an independent exercise, replace NaN with the string "NaN". Both methods can find that string when the query is the same string. The label looks similar on a screen but is a different value type. The exercise tests whether the learner can reason from values and comparison rules instead of trusting a printed label alone.

A useful report therefore records the query's type, the actual stored value and the comparison operation. Writing invalid reading absent after an indexOf failure would make a claim the observation does not support. The correct explanation preserves the existing NaN entry and identifies why that particular search did not count it as equal.

Contents · Previous chapter · Next chapter

CHAPTER 4 OF 14 · Inspect values and presence

4. Keep signed zero and object identity in view

Back to contents

The includes and indexOf comparison contracts both treat positive zero and negative zero as matching in the ordinary numeric searches considered here. A search that succeeds therefore does not establish which signed zero was stored. If a specialized calculation needs to preserve that distinction, choose an inspection rule that actually distinguishes it.

This does not mean every equality mechanism treats all exceptional numbers alike. The NaN difference from the previous chapter is specific. A programmer should not derive an entire comparison table from one memorable case. Record the relevant cases and use the comparison contract of the actual operation being called.

Now create two separate plain objects, each with an identifier property containing the same catalogue code. Put only the first object in an array. Searching with that same object reference succeeds. Searching with the separately created lookalike object fails for both methods. Matching field values do not make two ordinary object identities equal under these searches.

For a catalogue lookup by identifier, an explicit predicate or index keyed by the intended identifier can express the task. Simply wrapping a submitted identifier inside a new object and passing that object to includes asks a different question: whether that exact object reference appears in the sequence.

The distinction becomes important after copying. A shallow array copy can still contain the original object references, whereas separately reconstructing each record creates new identities. Two arrays can display equivalent-looking records yet produce different results when queried with an earlier reference. The operation's object identity rule remains consistent across those arrangements.

For transfer, retain a reference to the first record, change one of its properties and search again using that reference. The ordinary identity match can remain successful despite the changed field. Then construct another record with the new field value and search for it. Explain why visible similarity and identity membership are independent observations.

Contents · Previous chapter · Next chapter

CHAPTER 5 OF 14 · Inspect values and presence

5. Describe an empty slot precisely

Back to contents

Create an ordinary array with length three and assign a value only at index zero. The other two indexed properties are absent under the stated ordinary prototype conditions. The array has positions within its length without having own entries at every one of those positions. Calling them all undefined values would erase a useful distinction.

Compare a second three-position array whose last two entries were explicitly assigned undefined. Its length can match the first array's length, and a simple display can make the arrays look similar. Yet property-presence checks can distinguish the absent positions from the present entries containing undefined.

This matters because different array operations have different visitation rules. Some inspect presence before obtaining a value; others obtain a value directly for each position. An array's length alone does not tell you how many own indexed properties it has or how many callbacks another operation will invoke.

Use a table on paper with index, own-property presence and obtained value. For a genuinely absent ordinary position with no inherited indexed property, reading it yields undefined. That read result does not establish that an own property storing undefined existed beforehand. The access operation can produce the same value from a different source state.

The sort and Array.from specialists develop other consequences of this boundary. Here the reader job is narrower: explain why membership and first-position searches can disagree about undefined. Preserve the actual source shape while following the operation rather than replacing a sparse array with a visually similar dense one before testing.

For a changed-context check, assign undefined explicitly to one missing position and leave the other absent. Check length, property presence and read results separately. This gives the learner a three-part description that survives a misleading console presentation: the position is within range, the property may be absent and the read can still yield undefined.

Contents · Previous chapter · Next chapter

CHAPTER 6 OF 14 · Inspect values and presence

6. Compare undefined searches on sparse arrays

Back to contents

Take a fresh array created with one missing position and no inherited indexed value at that position. An includes search for undefined returns true. The operation reads the position, obtains undefined and compares it under its own equality rule. It does not first require an existing indexed property in the way indexOf does.

On that same sparse input, indexOf searching for undefined returns minus one. Its presence check finds no property to inspect at that position, so there is no matching present entry. The result does not prove that reading the position would produce some other value. It reflects a different route through the source shape.

Now replace the missing slot with an explicit undefined entry. Includes still returns true, while indexOf can now return zero. The difference between the two fixtures is property presence, not a changed query and not a changed array length. This makes a compact and checkable explanation of the two mechanisms.

const missing = new Array(1);
const present = [undefined];
missing.includes(undefined); // true
missing.indexOf(undefined); // -1
present.indexOf(undefined); // 0

For a missing-data audit, includes(undefined) is therefore not a complete count or classification of explicitly undefined entries. It can succeed because of a missing ordinary slot. If the website needs to distinguish an omitted measurement from a supplied undefined value, inspect the corresponding property-presence condition deliberately.

This is also why converting the array before checking can alter the diagnostic. A helper that materialises missing positions as present undefined entries changes the input to indexOf. The later method can be correct while answering a question about the transformed array rather than the original sparse record. Keep that transformation in the evidence ledger instead of silently treating the two sources as identical.

Contents · Previous chapter · Next chapter

CHAPTER 7 OF 14 · Trace the searched range

7. Work a mixed six-position ledger

Back to contents

Build an ordinary six-position array with twelve at zero, NaN at one, a missing slot at two, explicit undefined at three, the string twelve at four and numeric zero at five. Its length is six. Five own indexed entries exist. Those counts describe different properties of the same source.

Searching for numeric twelve produces membership true and first index zero. Searching for the string twelve produces true and four. The comparison does not merge those two representations. Searching for NaN produces true through includes and minus one through indexOf because equality, rather than position presence, determines that disagreement.

Searching for undefined with includes succeeds when it reads the missing position at two. IndexOf skips that genuinely absent property and finds the explicit undefined at three. Both methods indicate a successful membership interpretation, but the first observed source position differs. Includes does not expose that first position in its Boolean result.

This ledger helps separate two independent axes. The NaN case changes whether the compared values count as equal. The missing-slot case changes whether a position is considered for a value comparison through the same visit route. Explaining every disagreement as an equality issue would miss the presence check; explaining every disagreement as sparse-array handling would miss NaN.

Now delete the explicit undefined entry at three. The length remains six, but the own-property count drops. Includes(undefined) can still succeed through an ordinary missing position, while indexOf(undefined) now returns minus one under the fixture's prototype assumptions. Current shape, not an earlier printed array, determines the result.

For independent transfer, fill position two with a string and restore explicit undefined at three. Then search from index three rather than from the beginning. Predict the effects of source shape and starting boundary separately. The exercise is complete only when the learner can identify the value type, presence condition, searched range and returned result contract for each query.

Contents · Previous chapter · Next chapter

CHAPTER 8 OF 14 · Trace the searched range

8. Treat fromIndex as a search boundary

Back to contents

Work a five-entry route list containing west, east, north, east and south. Searching for east from zero yields index one. Searching from two yields index three. Searching from minus one begins at four and fails; it does not travel backwards to the east at three. Starting at five also fails because that boundary is beyond every existing position. In all four cases the list itself remains in its original order. The changed result comes from the eligible range, not a mutation or a different comparison of the label. A useful diagnostic records the interpreted start beside each result so the caller can see exactly which occurrence was excluded.

Both methods accept an optional starting-position argument. In the ordinary finite examples, a positive start selects the earliest index eligible for the forward search. It does not rotate the array or restrict the query to exactly one position. Entries before that boundary are simply outside the searched range.

Use the array of numbers fourteen, twenty-eight and fourteen again. Starting at index one excludes the first fourteen but still permits the later fourteen at two. Includes returns true for fourteen, and indexOf returns two. The numeric result remains an index in the original sequence, not an index relative to the shortened search range.

A negative starting argument is interpreted relative to the length under the operation's conversion rules. With length three, minus one selects the last position as the start of a forward search. It is not an instruction to search backwards. An eligible match earlier than that boundary remains excluded even though a human may read negative as reverse.

A start at or beyond the length leaves no eligible position in these examples. The ordinary results are false and minus one. A sufficiently negative start is clamped to the beginning. These boundary cases should be checked explicitly when a caller calculates a position from a previous operation rather than passing a simple literal.

The conversion of the starting argument is itself part of the contract. Avoid presenting an arbitrary object with side effects as though it were merely a stored number. For the central lesson, use finite integer starts so equality and property presence remain the focus. More general conversion cases require their own carefully bounded observations.

For transfer, use a five-position sequence with a repeated label at one and four. Predict results from zero, two, minus one and five. State that all searches proceed forward from the interpreted boundary. This stops a correct arithmetic calculation of the start from being combined with an invented reverse-search rule.

Contents · Previous chapter · Next chapter

CHAPTER 9 OF 14 · Trace the searched range

9. Make inherited indexed values explicit

Back to contents

The sparse examples assumed an ordinary prototype chain without a value at the missing index. Property access and property existence can also consult inherited properties. A statement that indexOf always skips every index lacking an own property would therefore be too strong. Own absence and complete property absence are different conditions.

Use an isolated array-like object with length one and an inherited property named zero containing a catalogue label. Borrow the ordinary array methods through call. Both operations can find that inherited value under their respective property-access routes. The fixture need not modify Array.prototype or affect unrelated arrays in the runtime.

This boundary matters when a program treats a search result as evidence of the source's own stored entries. A successful result can reflect a lookup through the prototype chain in the stated generic operation. If the actual policy concerns only own properties, implement and test that policy rather than assuming a generic membership call supplied it.

Keep the inherited-property experiment separate from the baseline arrays. Global prototype modifications can contaminate later tests and conceal their preconditions. An isolated object with a chosen prototype makes the condition visible while keeping the rest of the lesson's ordinary sparse-array traces intact.

For a concrete changed context, remove the inherited zero property from the isolated prototype. The array-like object still has length one, but the ordinary read now yields undefined and the presence check finds no indexed property. Includes(undefined) and indexOf(undefined) can then diverge as in the earlier missing-slot example.

The transferable habit is to report which notion of presence was checked. Within length, own property, inherited property and read result are distinct facts. A learner who can name those facts can diagnose the result without claiming the source literally stored an own entry merely because a search obtained a value at that index.

Contents · Previous chapter · Next chapter

CHAPTER 10 OF 14 · Check effects and limits

10. Observe reads and early stopping without adding noise

Back to contents

Searches can obtain values through accessors on deliberately constructed objects. A getter can record that it was read, return a value or throw. The method's result should therefore not be described as a promise that the source was untouched. For ordinary data-only arrays, no such getter effect is introduced by the lesson's baseline setup.

Use an isolated two-position array-like object with a counted getter at zero returning the requested label and another counted getter at one. Both ordinary searches can stop after finding the first match. The second getter need not be read. A successful result does not prove that every later entry was inspected or validated.

Change the first getter to return another label. The second position can now be reached. If that getter throws, the operation can terminate with an exception rather than return its normal no-match sentinel. Preserve the reads already recorded before the exception; an error is not evidence that no observation happened.

These controlled effects also show why searching an arbitrary object can differ from searching a saved plain-data representation. The getter can calculate its answer at access time. A previously printed value is not a guarantee of what the later read will return. Keep the operation and source assumptions visible when diagnosing a live interface.

Do not use this example to invent a broad transactional guarantee. Neither includes nor indexOf rolls back arbitrary effects performed by user-defined getters. A consumer needing inert data should establish the appropriate representation and trust boundary before relying on a search as a pure observation.

For transfer, predict the getter log for a match at zero, a match at one and an exception at one. Then compare the normal result and the effect history separately. This tests the actual visitation boundary without requiring a browser event, network service or unbounded experiment.

Contents · Previous chapter · Next chapter

CHAPTER 11 OF 14 · Check effects and limits

11. Avoid turning membership into validation

Back to contents

Suppose the measurement array contains twelve, NaN and thirty. Includes(NaN) can help identify one known exceptional value, but it does not establish that the sequence contains only finite acceptable measurements. Infinity, unsupported types, missing positions or values outside an application range require the policy that actually defines validity.

Likewise, failure to find undefined does not prove a dense clean record. The search may have begun after the problematic position, used indexOf on missing slots or operated on a transformed representation. State the query and searched range before interpreting a negative answer as a general data-quality certificate.

For a catalogue identifier, an object-reference membership result does not establish that the identifier is unique, current or authorized for use. Those questions require the appropriate identifier and authority checks. Keep the mechanism's narrow result useful without claiming it performs unrequested or impossible business validation.

A practical reader route is to separate accepted input syntax, value conversion, structural checks and membership in an allowed set. Each step should have a defined failure outcome. If the service requires a numeric code from a fixed catalogue, accepting any string that loosely resembles a number is not justified merely because one later search succeeds.

For a controlled example, define the fictional accepted modes as the exact strings compact and detailed. The numeric value zero and an object with a mode property are outside that stated representation even if the application could coerce them into some label. The input policy should decide whether such conversion is permitted before searching.

For transfer, ask a learner to design three separate checks: whether a source contains NaN, whether every required indexed measurement is present and whether each present measurement meets a stated range. They should explain why no single membership call answers all three. The result is a clearer inspection plan, not a larger pile of unexplained conditionals.

Contents · Previous chapter · Next chapter

CHAPTER 12 OF 14 · Check effects and limits

12. Check original and changed-context fixtures

Back to contents

The controlled checks should begin with simple strings and repeated numbers. Assert both membership and first-index results, including index zero and the no-match sentinel. Those cases catch a mistaken truthiness condition before the unusual numeric and sparse-array boundaries are introduced.

Next assert the NaN disagreement, the shared treatment of signed zero and the lack of automatic number-string matching. Use separate ordinary object identities to check reference membership. A test that compares only printed output can miss both value type and object identity, so retain the actual values in the assertions.

Create fresh sparse and explicit-undefined arrays for the presence cases. Assert length and property presence as well as the two search results. Then exercise the mixed six-position ledger and its deletion change. This establishes that the explanation concerns the original source shape rather than an accidental dense copy.

Check positive, negative and beyond-length starts using finite integer inputs. Verify that returned indices remain positions in the original array. The changed-context fixture should move the matching entry so that a copied expected index cannot pass without understanding the searched range.

Finally use isolated inherited-property and counted-getter objects. Assert early stopping and the exception category separately from the recorded access history. These fixtures establish the ordinary generic contracts actually exercised; they do not certify every Proxy, subclass, overridden method or production application.

No network request or real submission is needed. The useful evidence is the runtime version, fixture definition, expected result and completed assertion. For a website using these operations, inspect its actual input conversion, array shape, prototype assumptions and conditional. Passing language fixtures supplies targeted questions for that review, not a claim that the whole visitor journey has been tested.

Contents · Previous chapter · Next chapter

CHAPTER 13 OF 14 · Transfer to a catalogue

13. Transfer the reasoning to a catalogue filter

Back to contents

A fictional catalogue lets a visitor choose a display mode, then filters measurement rows for a preview. Retain the selected mode as a validated string under a stated allowed-mode contract. A membership test can then answer whether that representation belongs to the permitted set without pretending to validate every later measurement.

For the preview rows, retain a distinction between missing positions and explicit unusable readings. If a missing position means no observation was supplied while NaN means a calculation produced no ordinary numeric result, the reporting interface should communicate those different states deliberately. A single blank cell can conceal a meaningful difference.

Suppose a developer replaces an explicit membership condition based on indexOf with includes. For ordinary strings that may preserve the intended Boolean behaviour. For NaN and sparse undefined searches, the result can change. A refactor should compare the actual accepted input domain rather than assuming the methods are universally interchangeable.

The reverse replacement also needs care. Converting includes(query) into indexOf(query) greater than or equal to zero changes the comparison and presence contracts despite its familiar shape. It is a suitable equivalence only under conditions that exclude the relevant differences. State those conditions and test the actual values crossing the boundary.

For an independent exercise, give the learner one dense list of allowed strings, one sparse measurement row and one list of object records. Ask for the appropriate operation and interpretation in each. The answer should name result type, equality, property presence and application meaning rather than simply nominate a favourite method.

This transfer is useful beyond a coding puzzle. It teaches how to preserve a reader's actual question while moving between representations. A catalogue filter can be mechanically correct and still communicate the wrong missing-data conclusion if its author substitutes a narrow search result for the broader claim the interface displays.

Contents · Previous chapter · Next chapter

CHAPTER 14 OF 14 · Transfer to a catalogue

14. Report the comparison and the source shape together

Back to contents

A precise report names the operation, query value and type, source length, relevant property-presence assumptions and starting boundary. It records a Boolean membership result or an index result without silently converting one into the other. For object values, it also states whether the query retains an existing reference or constructs a separate lookalike.

When the methods disagree about NaN, identify equality as the deciding boundary. When they disagree about an ordinary missing position and undefined, identify the presence-versus-read route. When a condition behaves incorrectly at index zero, identify the mistaken interpretation of the numeric result. These are different faults with different concrete repairs.

For a negative result, state the searched range. For a positive result, avoid implying a count, uniqueness guarantee or validation of later entries. If accessors or inherited properties were involved, preserve those conditions in the evidence rather than attributing the observed value to an own data entry that never existed.

The final independent check uses a fresh source and a changed query. Predict both results before execution, then explain each through the contract. If the explanation relies only on a remembered output from the six-position example, change the position or type again until the reasoning transfers.

A good repair makes the intended question visible in the code and the report. Membership, first position, structural presence and data validity can all be useful results. They become misleading when one is presented as proof of another without the required operation or evidence.

The lasting habit is to keep comparison and representation separate. Once the value, equality rule, eligible position and return contract are named, a surprising search outcome becomes traceable. The website can then preserve the visitor's intended choice and describe missing information accurately without asking one convenient method to answer every question about the data.

Contents · Previous chapter · Return to Websites

Tracing Default Array Sort Order Through Numbers, Undefined Values and Empty Slots

Tracing Default Array Sort Order Through Numbers, Undefined Values and Empty Slots. See another operation where present undefined values and missing indexed properties have different consequences.

Tracing Array.from Mapping Before Source Consumption Finishes

Tracing Array.from Mapping Before Source Consumption Finishes. Check whether a prior materialisation step changed the sparse source before you searched it.

Tracing Map Visits When Entries Are Deleted and Reinserted

Tracing Map Visits When Entries Are Deleted and Reinserted. Separate object-key identity, current membership and traversal history in a different collection contract.

Flattening depth can change the searched representation. Trace omitted sparse positions and explicit undefined before applying a later membership test.

Reduction distinguishes source presence and seed selection. Compare another array contract in which a value can be obtained without becoming a current callback argument.

Frequently asked questions

Why does includes find NaN while indexOf does not?

Ordinary includes uses SameValueZero comparison, which matches NaN in this case. IndexOf uses strict equality, under which NaN does not equal itself.

Does includes(undefined) prove an undefined entry was assigned?

No. On an ordinary sparse array, reading a missing position can yield undefined and satisfy includes without an own indexed entry storing it.

Can I use indexOf directly as an if condition?

Its index-zero success is falsy and its minus-one failure is truthy. Interpret the documented sentinel explicitly or use a suitable membership operation.

Do negative starting positions search backwards?

No. They establish a starting boundary relative to length under the conversion rules. These searches still proceed forward.

Do matching object fields guarantee a match?

No. These ordinary object searches use reference identity. A separate lookalike record is not the same object.

Do the checks validate a production catalogue?

They check the stated language fixtures. The actual input, transformation, source shape and visitor-facing decision still require application-specific verification.

Authoritative sources and further reading

ECMAScript: Array includes. The normative SameValueZero comparison and direct positional reads.

ECMAScript: Array indexOf. The strict-equality search, property-presence check and returned position.

Return to the Websites subject guide, The Gold Standard of Education, or the How X Works hub.