Storage

localStorage.length

Returns the number of key/value pairs currently present in the localStorage object

Syntax

JavaScript
localStorage.length

Return Value

An integer representing the number of stored items

Examples

Basic Usage
console.log('Stored items:', localStorage.length)
Practical Example
function isStorageEmpty(): boolean {
  return localStorage.length === 0
}
Advanced Usage
localStorage.setItem('a', '1')
localStorage.setItem('b', '2')
console.log(localStorage.length) // 2

Understanding localStorage.length

The localStorage.length method in JavaScript returns the number of key/value pairs currently present in the localStorage object. It belongs to the Storage object and is one of the most widely used methods for working with storage values in modern JavaScript and TypeScript applications.

The method signature is localStorage.length. When called, it returns an integer representing the number of stored items. Understanding when and how to use length() helps you write more expressive, readable code.

Common use cases for localStorage.length include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like localstorage-key, localstorage-clear, localstorage-getitem, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for localStorage.length is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Storage Methods

Other methods in the Storage object

Related Tools

More Storage Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.