# How to get a array lengh?

**URL:** <https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346>\
**Category:** Language Support\
**Tags:** Motoko\
**Created:** [November 4, 2021, 3:18pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346 "2021-11-04T15:18:47Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![bytesun](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/bytesun/32/2682_2.png) [@bytesun](https://forum.dfinity.org/u/bytesun)\
**Post date:** [November 4, 2021, 3:18pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/1 "2021-11-04T15:18:48Z")

</div>

is there a method or attribute like js (arr.length) ?

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [November 4, 2021, 4:56pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/2 "2021-11-04T16:56:35Z")

</div>

Try this:

```auto
arr.size()

```

---

<div class="post-metadata">

**Author:** ![wewei](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/wewei/32/4486_2.png) [@wewei](https://forum.dfinity.org/u/wewei)\
**Post date:** [February 24, 2022, 4:06pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/3 "2022-02-24T16:06:01Z")

</div>

It’s strange such a fundamental API is not documented, (or hard to find in the document)

---

<div class="post-metadata">

**Author:** ![diegop](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/diegop/32/569_2.png) [@diegop](https://forum.dfinity.org/u/diegop)\
**Post date:** [February 25, 2022, 2:29pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/4 "2022-02-25T14:29:25Z")

</div>

You are totally right.

I was surprised to find that it was not here: [Array :: Internet Computer](https://smartcontracts.org/docs/base-libraries/Array.html)

Short term: i’ll make sure its added.

Medium term: we are in the middle of an entire overhaul of the documentation (because none of us are happy with it)

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [February 25, 2022, 3:03pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/5 "2022-02-25T15:03:48Z")

</div>

> [@diegop](#):
>
> I was surprised to find that it was not here: [Array :: Internet Computer](https://smartcontracts.org/docs/base-libraries/Array.html)

Because `.size()` is actually not defined in the base library, but comes directly from the compiler. Having said that, I fully agree that in the name of discoverability we should at least mention it in that module (just like the generated `==` operator, when the compiler is able to generate it).

Edit: I had to dive a bit into the docs and find it buried deep in the language overview chapter of the Motoko guide: [Smart Contracts on the Internet Computer](https://smartcontracts.org/docs/language-guide/overview.html#_arrays_immutable)

So it is there, but we could do better. The search engine for the site also didn’t come up with this.

---

<div class="post-metadata">

**Author:** ![diegop](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/diegop/32/569_2.png) [@diegop](https://forum.dfinity.org/u/diegop)\
**Post date:** [February 25, 2022, 4:27pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/6 "2022-02-25T16:27:50Z")

</div>

Thank you, Gabor. This is helpful

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [February 25, 2022, 9:20pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/7 "2022-02-25T21:20:14Z")

</div>

I think a simple short-term fix would be to add a wrapper to the `Array` module. Something like:

```auto
public func size<A>(xs : [A]) : Nat = xs.size();

```

Same goes for other things where this comes up.

---

<div class="post-metadata">

**Author:** ![Mathias](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@Mathias](https://forum.dfinity.org/u/Mathias)\
**Post date:** [December 28, 2022, 8:48am UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/8 "2022-12-28T08:48:51Z")

</div>

This is not in the docs yet right? [Internet Computer Content Validation Bootstrap](https://internetcomputer.org/docs/current/references/motoko-ref/Array)

@diegop

Can you track how devs interact with the docs? I’d be so curious to see what they run into.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [December 28, 2022, 6:25pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/9 "2022-12-28T18:25:42Z")

</div>

I made a PR:

> <https://github.com/dfinity/motoko-base/pull/486>
>
> This aims to address https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346 a…nd other instances of people asking about how to get the size of an array due to it being omitted from the generated documentation.

---

<div class="post-metadata">

**Author:** ![ggreif](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ggreif/32/301_2.png) [@ggreif](https://forum.dfinity.org/u/ggreif)\
**Post date:** [January 20, 2023, 5:47pm UTC](https://forum.dfinity.org/t/how-to-get-a-array-lengh/8346/10 "2023-01-20T17:47:11Z")

</div>

`Array.size()` is released now: [Release 0.7.6 · dfinity/motoko · GitHub](https://github.com/dfinity/motoko/releases/tag/0.7.6)  
Please note that `dfx` may need a few days to catch up.

Edit: `dfx-0.13.0-beta.1` has caught up!
