localStorage.length
Returns the number of key/value pairs currently present in the localStorage object
Syntax
localStorage.lengthReturn Value
An integer representing the number of stored items
Examples
console.log('Stored items:', localStorage.length)function isStorageEmpty(): boolean {
return localStorage.length === 0
}localStorage.setItem('a', '1')
localStorage.setItem('b', '2')
console.log(localStorage.length) // 2Understanding 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
localStorage.keyReturns the name of the nth key in the localStorage, or null if the index is out of range
localStorage.clearRemoves all key/value pairs from the localStorage object
localStorage.getItemReturns the value associated with the given key in the localStorage object, or null if the key does not exist
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.