{"id":13484,"library":"lodash.last","title":"lodash.last","description":"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.","status":"active","version":"3.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/lodash/lodash","tags":["javascript","lodash","lodash-modularized","stdlib","util"],"install":[{"cmd":"npm install lodash.last","lang":"bash","label":"npm"},{"cmd":"yarn add lodash.last","lang":"bash","label":"yarn"},{"cmd":"pnpm add lodash.last","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Standard CommonJS import for Node.js environments. This package exports the `last` function directly as its module.exports.","symbol":"last","correct":"const last = require('lodash.last');"},{"note":"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.","wrong":"import { last } from 'lodash.last';","symbol":"last","correct":"import last from 'lodash.last';"},{"note":"`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.","symbol":"last (TypeScript)","correct":"import last from 'lodash.last';\nconst data: number[] = [1, 2, 3];\nconst lastElement: number | undefined = last(data);"}],"quickstart":{"code":"import last from 'lodash.last';\n\nconst numbers: number[] = [1, 2, 3, 4, 5];\nconst lastNumber: number | undefined = last(numbers);\nconsole.log(`The last number is: ${lastNumber}`);\n// Expected output: The last number is: 5\n\nconst emptyArray: any[] = [];\nconst lastOfEmpty: any | undefined = last(emptyArray);\nconsole.log(`The last element of an empty array is: ${lastOfEmpty}`);\n// Expected output: The last element of an empty array is: undefined\n\nconst mixedArray = [1, 'hello', true, { key: 'value' }];\nconst lastMixedElement: any | undefined = last(mixedArray);\nconsole.log(`The last element of a mixed array is: ${lastMixedElement?.key ?? String(lastMixedElement)}`);\n// Expected output: The last element of a mixed array is: value (if object, else its string representation)","lang":"typescript","description":"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."},"warnings":[{"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.","message":"Lodash 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.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Upgrade `lodash` and related modular packages to `4.18.0` or higher to include security patches. Regularly audit your dependencies for known vulnerabilities.","message":"Lodash 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.","severity":"gotcha","affected_versions":"<4.18.0"},{"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.","message":"Modular 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.","severity":"gotcha","affected_versions":">=4.0.0 <4.18.1"},{"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.","message":"Mixing 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.","severity":"gotcha","affected_versions":"*"},{"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.","message":"While 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.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"search_vec":"'4.17':64 'abil':106 'adher':41 'aid':116 'align':49 'applic':119 'array':27,32 'bug':86 'bundl':120 'cadenc':84 'compar':122 'compat':77 'consist':82 'current':55 'design':19 'differenti':97 'ecosystem':39 'element':24 'empti':31 'ensur':76 'entir':126 'environ':133 'extens':37 'fix':87 'focus':5 'follow':72 'function':11,101 'general':48 'implement':7 'import':108,124 'javascript':132,139 'key':96 'last':10,23 'leverag':135 'librari':16,62,128 'like':69,102 'lodash':14,38,52,61,78,100,127,140,142 'lodash-modular':141 'lodash.last':1,2,40,70,103 'main':60 'maintain':80 'major':51,74 'modern':131 'modular':6,67,99,143 'necessari':111 'packag':68 'part':34 'particular':129 'patch':90 'provid':3 'reduc':118 'releas':53,83 'retriev':21 'return':28 'secur':89 'semant':43 'seri':94 'shake':138 'signific':115 'size':121 'stabl':56 'stdlib':144 'tree':137 'tree-shak':136 'typic':71 'undefin':29 'util':15,112,145 'v4':93 'version':44,47,57,75 'within':91 'x':65","created_at":"2026-04-20T01:54:44.440161+00:00","updated_at":"2026-04-20T01:54:44.440161+00:00","problems":[{"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.","cause":"An internal dependency within a modular Lodash build (or Lodash itself) was incorrectly mapped or missing due to a build defect.","error":"ReferenceError: [some_lodash_internal_function] is not defined"},{"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');`.","cause":"Incorrect ES module import syntax for `lodash.last`, attempting to destructure a default export, or importing from the wrong path.","error":"TypeError: last is not a function"},{"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`).","cause":"TypeScript compiler cannot locate the type definitions for `lodash.last` or the package itself is not installed.","error":"TS2307: Cannot find module 'lodash.last' or its corresponding type declarations."}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":"https://lodash.com","github":"https://github.com/lodash/lodash","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/lodash.last","openapi_spec":null,"status_page":null,"smithery":null,"categories":["data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-18","install_tag":null}}