Skip to content

Bug: Falsy thisArg (0, '', false) overridden in arrForEach/iterForOf/objForEachKey #561

@MSNev

Description

@MSNev

Description

Several iteration functions use truthy checks for thisArg fallback, which means legitimate falsy values (0, '', false, null) are incorrectly overridden.

Affected locations:

  • lib/src/array/forEach.ts line 58
  • lib/src/iterator/forOf.ts line 71
  • lib/src/object/for_each_key.ts

Impact

When a user passes 0, empty string, or false as thisArg, the function silently replaces it with the array/object being iterated, leading to incorrect this context in callbacks.

Steps to Reproduce

javascript arrForEach([1,2,3], function(v) { console.log(this); // Expected: 0, Actual: the array }, 0);

Suggested Fix

Use nullish coalescing (??) or explicit undefined check instead of truthy check:

ypescript // Instead of: thisArg || theArray thisArg !== undefined ? thisArg : theArray

Severity

Medium — edge case but violates expected JavaScript semantics for thisArg.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions