Registry / data / lodash.last

lodash.last

JSON →
library3.0.0jsnpmunverified

lodash.last provides a focused, modularized implementation of the `_.last` function from the Lodash utility library. It is designed to retrieve the last element of an array, returning `undefined` for empty arrays. As part of the extensive Lodash ecosystem, `lodash.last` adheres to semantic versioning, with its versioning generally aligning with major Lodash releases. The current stable version of the main Lodash library is 4.17.x, and modular packages like `lodash.last` typically follow this major version, ensuring compatibility. Lodash itself maintains a consistent release cadence for bug fixes and security patches within its v4 series. A key differentiator for modular Lodash functions like `lodash.last` is the ability to import only the necessary utilities, which can significantly aid in reducing application bundle sizes compared to importing the entire `lodash` library, particularly in modern JavaScript environments that leverage tree-shaking.

npm install lodash.last
INSTALL
IMPORT
SIG · LODASH.LAST
L
lodash.last
datajavascriptv3.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

last
✓ const last = require('lodash.last');
Standard CommonJS import for Node.js environments. This package exports the `last` function directly as its module.exports.
last
✓ import last from 'lodash.last';
✗ import { last } from 'lodash.last';
ES Module import. `lodash.last` exports a default function, not named exports. Attempting to destructure it with `{ last }` will result in a `TypeError`. For optimal tree-shaking with the main `lodash` package, `import { last } from 'lodash';` is often preferred if a modern bundler is used.
last (TypeScript)
✓ import last from 'lodash.last'; const data: number[] = [1, 2, 3]; const lastElement: number | undefined = last(data);
`lodash.last` ships with its own type definitions, allowing direct type inference when used in TypeScript projects. No separate `@types/lodash.last` package is typically required.

Demonstrates how to import and use the `last` function to retrieve the final element from various arrays, including handling empty arrays and showing type inference in TypeScript.

import last from 'lodash.last'; const numbers: number[] = [1, 2, 3, 4, 5]; const lastNumber: number | undefined = last(numbers); console.log(`The last number is: ${lastNumber}`); // Expected output: The last number is: 5 const emptyArray: any[] = []; const lastOfEmpty: any | undefined = last(emptyArray); console.log(`The last element of an empty array is: ${lastOfEmpty}`); // Expected output: The last element of an empty array is: undefined const mixedArray = [1, 'hello', true, { key: 'value' }]; const lastMixedElement: any | undefined = last(mixedArray); console.log(`The last element of a mixed array is: ${lastMixedElement?.key ?? String(lastMixedElement)}`); // Expected output: The last element of a mixed array is: value (if object, else its string representation)
Debug
Known issues
breakingLodash v4.0.0 introduced significant breaking changes across the entire library. While `lodash.last` itself is a simple function, its context within larger Lodash applications might be affected by API changes in other utility functions, argument order changes, or altered iteration behaviors. Ensure all Lodash-related packages are compatible with v4 if upgrading.
fix
Review the Lodash v4 changelog for relevant breaking changes in other functions your application uses. Ensure modular packages like `lodash.last` are updated to versions compatible with your main `lodash` dependency, typically by using a `^` or `~` version range.
affects: >=4.0.0
gotchaLodash had a critical prototype pollution vulnerability (GHSA-f23m-r3pf-42rh) in `_.unset` and `_.omit` functions, affecting versions prior to 4.18.0. Although `lodash.last` is not directly vulnerable, it is crucial to ensure your `lodash` dependency, if also used, is updated to 4.18.0 or later to mitigate supply chain risks across your entire Lodash footprint.
fix
Upgrade `lodash` and related modular packages to `4.18.0` or higher to include security patches. Regularly audit your dependencies for known vulnerabilities.
affects: <4.18.0
gotchaModular Lodash builds, including `lodash.template` and `lodash.fromPairs`, experienced a `ReferenceError` in `lodash` and `lodash-es` distributions due to internal dependency mapping issues. While `lodash.last` wasn't explicitly named, issues affecting `lodash`'s build process could potentially impact other modular functions or indicate broader build fragility.
fix
Ensure all `lodash` related packages are updated to at least `4.18.1` to benefit from fixes to the internal build system and prevent potential runtime errors.
affects: >=4.0.0 <4.18.1
gotchaMixing CommonJS `require()` and ES module `import` syntax can lead to unexpected behavior or build errors, especially in modern JavaScript environments. `lodash.last` exports a default function, which changes how it's imported in ESM versus CJS.
fix
For CommonJS, use `const last = require('lodash.last');`. For ES Modules, use `import last from 'lodash.last';`. Avoid `import { last } from 'lodash.last';` as it is not a named export. Ensure your build configuration (e.g., Webpack, Rollup) correctly handles module resolution.
affects: *
gotchaWhile modular imports like `lodash.last` are intended to reduce bundle size by only including specific functions, their effectiveness depends on the overall project setup and build tooling. In some cases, importing individual modular packages might not always result in a smaller bundle compared to importing from `lodash-es` if tree-shaking isn't fully optimized, or if many individual modular packages are used.
fix
Use a bundle analyzer (e.g., Webpack Bundle Analyzer) to verify the actual impact on bundle size. For modern applications, consider `import { last } from 'lodash-es';` or `import { last } from 'lodash';` and rely on a robust tree-shaking build setup, which is often more efficient than separate modular packages.
affects: *
Errors
Common errors & fixes
ReferenceError: [some_lodash_internal_function] is not defined
An internal dependency within a modular Lodash build (or Lodash itself) was incorrectly mapped or missing due to a build defect.
fix
Update `lodash.last` and any other `lodash` or `lodash-es` packages to version `4.18.1` or higher, which contains fixes for internal dependency resolution issues.
TypeError: last is not a function
Incorrect ES module import syntax for `lodash.last`, attempting to destructure a default export, or importing from the wrong path.
fix
For ES Modules, ensure you are using `import last from 'lodash.last';` as it is a default export. If using CommonJS, confirm `const last = require('lodash.last');`.
TS2307: Cannot find module 'lodash.last' or its corresponding type declarations.
TypeScript compiler cannot locate the type definitions for `lodash.last` or the package itself is not installed.
fix
Ensure `lodash.last` is installed (`npm install lodash.last`). Type definitions are usually bundled, but if not, try installing `@types/lodash` if available and needed (`npm install --save-dev @types/lodash`).
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
lodash.last — npm install lodash.last · libregistry