Search code examples
javascripttyped-arrays

Why is the value of Float32Array.length always 3?


I was reading the Mozilla Developer Network docs on Float32Arrays when I came upon

Float32Array.length
Length property whose value is 3.

... why is always 3? I also noticed that prototype property of the same name overrides it.


Solution

  • It's because the constructor takes up to 3 arguments:

    Float32Array(buffer [, byteOffset [, length]]);
    

    Every function in JavaScript has a length property that will return the count of the named parameters it takes.

    E.g.

    function foo(a, b) {}
    foo.length === 2; // true
    
    function bar() {}
    bar.length === 0; // true