JavaScript length

Beginner
6 min read
Updated: Nov 2026

Definition and Usage

storage.length is the number of keys currently stored.

JavaScript Syntax

JavaScript
const store = new Map();
const storage = {
  setItem(k,v){store.set(k,String(v));},
  getItem(k){return store.has(k)?store.get(k):null;},
  removeItem(k){store.delete(k);},
  clear(){store.clear();},
  key(i){return [...store.keys()][i]||null;},
  get length(){return store.size;}
};
storage.setItem("a", "1");
console.log(storage.length);
storage.removeItem("a");
console.log(storage.length);
Try it Editor

How It Works

  1. After set. length is 1.
  2. After remove. length is 0.

Examples

Example 1 — Read-only

You don't assign length.

JavaScript
console.log("use clear/removeItem to shrink storage");
Try it Editor

How It Works

  1. length reflects contents.

Frequently Asked Questions

No—it is a data property.

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful