You could use the valueOf hack:

this.length = {
    'valueOf': function (){
        return A.length;
    },
    'toString': function (){
        return A.length;
    }
};

Now you can access the length as x.length. (Although, maybe it's just me, but to me, something about this method feels very roundabout, and it's easy enough to go with a sturdier solution and, for example, update the length property after every modification.)

Answer from Casey Chu on Stack Overflow
People also ask

Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds top-level await, private class fields with #, and the .at() array method. For older browsers, transpile with Babel or use Vite, esbuild, or webpack.
🌐
itsourcecode.com
itsourcecode.com › home › exploring what is javascript set length and how to use it
Exploring What Is Javascript Set Length And How To Use It
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard with IntelliSense, debugger, terminal, Git, and a huge extension marketplace. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
🌐
itsourcecode.com
itsourcecode.com › home › exploring what is javascript set length and how to use it
Exploring What Is Javascript Set Length And How To Use It
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools (F12), Console tab, paste code, Enter. For HTML pages, add a script tag pointing to your .js file. Locally: install Node.js (LTS), then node script.js in your terminal from the file folder.
🌐
itsourcecode.com
itsourcecode.com › home › exploring what is javascript set length and how to use it
Exploring What Is Javascript Set Length And How To Use It
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-set-size-property
JavaScript Set size Property - GeeksforGeeks
July 23, 2025 - The Set.size property in JavaScript returns the number of elements in a Set. In other words, we can say that this property is used to get the current size of the set.
Find elsewhere
🌐
TutorialKart
tutorialkart.com › javascript › javascript-set-size-length
How to get Length/Size of a Set in JavaScript?
October 1, 2021 - To get length or size of a Set in JavaScript, access size property of this Set. size is a read only property of Set object and returns an integer value
🌐
Rip Tutorial
riptutorial.com › getting set length
JavaScript Tutorial => Getting set length
You can get the number of elements inside the set using the .size property · This property, unlike Array.prototype.length, is read-only, which means that you can't change it by assigning something to it:
🌐
Itsourcecode
itsourcecode.com › home › exploring what is javascript set length and how to use it
Exploring What Is Javascript Set Length And How To Use It
July 10, 2026 - Learn What is Javascript Set Length And How To Use It with the help of various methods and example program.
🌐
EyeHunts
tutorial.eyehunts.com › home › javascript set length | example code
JavaScript set length | Example code - Tutorial - By EyeHunts
August 8, 2022 - const set1 = new Set(); const object1 = {}; set1.add(42); set1.add('forty two'); set1.add('forty two'); set1.add(object1); console.log(set1.size); // 3 · This property, unlike Array.prototype.length, is read-only, which means that you can’t change it by assigning something to it:
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set
Set - JavaScript - MDN Web Docs
August 13, 2026 - const array = Array.from(document.querySelectorAll("[id]")).map((e) => e.id); const set = new Set(array); console.assert(set.size === array.length);
Top answer
1 of 5
12

It turns out the length property on functions is configurable, which means you can use .defineProperty to change the value of length on a function. Example:

function hi() {}
hi.length === 0; // Expected

Object.defineProperty(hi, "length", { value: 5 })
hi.length === 5; // Intriguing

This works in the latest version of Chrome and Firefox, but it does not work in Safari (v9.1.1).

2 of 5
6

For now, here's the best solution I could think of.

makeFunc = function (length, fn) {
    switch (length) {
    case 0 : return function () { return fn.apply(this, arguments); };
    case 1 : return function (a) { return fn.apply(this, arguments); };
    case 2 : return function (a,b) { return fn.apply(this, arguments); };
    case 3 : return function (a,b,c) { return fn.apply(this, arguments); };
    case 4 : return function (a,b,c,d) { return fn.apply(this, arguments); };
    case 5 : return function (a,b,c,d,e) { return fn.apply(this, arguments); };
    case 6 : return function (a,b,c,d,e,f) { return fn.apply(this, arguments); };
    case 7 : return function (a,b,c,d,e,f,g) { return fn.apply(this, arguments); };
    case 8 : return function (a,b,c,d,e,f,g,h) { return fn.apply(this, arguments); };
    case 9 : return function (a,b,c,d,e,f,g,h,i) { return fn.apply(this, arguments); };
    default : return function (a,b,c,d,e,f,g,h,i,j) { return fn.apply(this, arguments); };
    }
};

Example usage:

var realFn = function () {
    return "blah";
};

lengthSix = makeFunc(6, realFn);

lengthSix.length; // 6
lengthSix(); // "blah"

Personally, I always cringe whenever I use copy and paste when programming, so I'd be very happy to hear of any better options.

Update

I thought of a method which could work for any arbitrary size, unlike the example above which is limited by how many times you want to copy-and-paste. Essentially, it dynamically creates a function (using new Function) which will return a function of the right size which then just proxies through to whatever function you pass to it. Yeah that does hurt your head. Anyway, I thought I'd benchmark it against the above...

http://jsperf.com/functions-with-custom-length (you can see the 'evil' code there too).

The evil method is many hundreds of times slower than the hacky copypasta method, so there you go.

🌐
freeCodeCamp
forum.freecodecamp.org › curriculum help
Size property!?!?! - Curriculum Help - The freeCodeCamp Forum
July 18, 2020 - So please forgive me if this question is super stupid, but can I think of the “size” in JS as like the length property, but for Sets?
🌐
Python Examples
pythonexamples.org › javascript › how-to-get-length-of-a-set
How to Get Length of a Set in JavaScript
To get the size of a set in JavaScript, you can use the size property of the set.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › length
Array: length - JavaScript - MDN Web Docs
May 2, 2026 - Setting any array index (a nonnegative integer smaller than 232) beyond the current length extends the array — the length property is increased to reflect the new highest index.
🌐
SitePoint
sitepoint.com › blog › javascript › do i use .size() or .length in javascript?
Do I use .size() or .length in Javascript? — SitePoint
February 12, 2024 - To determine the number of elements in an array, you should use the length property. You can determine the size of a Map or Set object in JavaScript using the size property.
Top answer
1 of 2
18

There's a lot of discussion in the esdiscuss thread "Set length property". This was a hotly debated issue, so it's not surprising that you do not necessarily agree with the resolution.

There is a tremendous amount of arguing about this in esdiscuss. Ultimately, the argument that prevailed (as evidenced by the fact that ES2015's Sets have size and not length) was summarized in a post by David Bruant:

...for me 'length' refers to a measurement with something like a ruler. You start at 0 and see up to where it goes. This is very accurate for an array which is an indexed set (starting at 0 and growing) and for arrays as considered in C (continuous sequence of bytes) which ECMAScript arrays seem inspired of. This is probably less relevant for unordered collections such as sets which I'd tend to consider as a messy bag.

And further discussed in a post by Dean Landolt:

Just wanted to jump in and say non-writable length is consistent with String behavior as well, but David makes a good point about length implying metric topology. David's suggestion of count is nice. ISTM what we're talking about is cardinality, but no need to get too silly w/ precision. Though size is just fine with me, and has plenty of prior art.

2 of 2
1

While apsillers' Jan 27, 2016 answer adds great links, a code example is missing. The size of a set is a read-only getter while that's not the case for arrays which allow modifying the length to truncate the array.

let arr = [1, 2, 3, 4]
arr.length = 2
console.log("modified length array", arr) // [1, 2]

let mySet = new Set([1, 2, 3, 4])
mySet.length = 2
mySet.size = 2
console.log("modified length set", [...mySet]) // [1, 2, 3, 4]

let str = "1234"
str.length = 2
console.log("modified length string", str) // "1234"

🌐
GitHub
github.com › ljharb › set-function-length
GitHub - ljharb/set-function-length: Set a function's length property · GitHub
var setFunctionLength = require('set-function-length'); var assert = require('assert'); function zero() {} function one(_) {} function two(_, __) {} assert.equal(zero.length, 0); assert.equal(one.length, 1); assert.equal(two.length, 2); assert.equal(setFunctionLength(zero, 10), zero); assert.equal(setFunctionLength(one, 11), one); assert.equal(setFunctionLength(two, 12), two); assert.equal(zero.length, 10); assert.equal(one.length, 11); assert.equal(two.length, 12);
Author: ljharb