Skip to content

[Edit] JavaScript: .length #7272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 85 additions & 9 deletions content/javascript/concepts/storage/terms/length/length.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,121 @@
---
Title: '.length'
Description: 'Returns the number of items in an array or the number of characters in a string.'
Description: 'Returns the number of elements, characters, or items in a given data structure.'
Subjects:
- 'Web Development'
- 'Computer Science'
- 'Web Development'
Tags:
- 'Arrays'
- 'Data'
- 'Elements'
- 'Strings'
- 'Arrays'
CatalogContent:
- 'introduction-to-javascript'
- 'paths/front-end-engineer-career-path'
---

The **`.length`** property is used on arrays and strings to quickly find out how many items are in an array or how many characters are in a string. This property is straightforward to use and plays a crucial role in JavaScript, making it simple for developers to work with the data.
In JavaScript, the **`.length`** property is used to determine the number of elements, characters, or items in a given data structure, such as arrays or strings. This property is straightforward to use and plays a crucial role in JavaScript, making it simple for developers to work with the data.

## Syntax

```pseudo
myString.length
```

> **Note**: The `.length` property is not a method but a property. It cannot be invoked with parentheses, like `.length()`. Instead, it can only be referenced as `.length`.
**Parameters:**

The `.length` property does not accept any parameters.

**Return value:**

Returns the number of elements, characters, or items in a given data structure.

## Example
## Example 1: Using `.length` with Arrays

The `.length` property is applied to an array and a string, respectively, to quickly determine their sizes (number of elements for an array and number of characters for a string). This property provides a convenient and efficient way to work with data in JavaScript.
This example uses `.length` to calculate the total number of elements in an array:

```js
const fruits = ['apple', 'banana', 'orange', 'grape'];

const numberOfFruits = fruits.length;

console.log(numberOfFruits);
```

Here is the output:

```shell
4
```

## Example 2: Using `.length` with Strings

This example uses `.length` to calculate the total number of characters in a string:

```js
const message = 'Hello, world!';

const messageLength = message.length;

console.log(messageLength);
```

This example results in the following output:
Here is the output:

```shell
4
13
```

## Example 3: Using `.length` with DOM Collections

When working with the DOM (Document Object Model), methods like `document.querySelectorAll()` return NodeLists, which are array-like. In this case, `.length` can be used to count the total number of selected elements:

```js
const buttons = document.querySelectorAll('button');

console.log(buttons.length);
```

Here is a possible output:

```shell
2
```

## Frequently Asked Questions

### 1. Can I use `.length` with objects?

Not directly. Standard JavaScript objects don't have a `.length` property. To count the number of keys, use:

```js
const obj = { a: 1, b: 2 };

console.log(Object.keys(obj).length); // Output: 2
```

### 2. Does `.length` count undefined or empty slots in arrays?

Yes. JavaScript arrays can be sparse, and `.length` will count the total index range, not just defined elements:

```js
const arr = [];

arr[3] = 'dog';

console.log(arr.length); // Output: 4
```

### 3. Is `.length` writable for arrays?

Yes, but with caution:

```js
const arr = [1, 2, 3, 4];

arr.length = 2;

console.log(arr); // Output: [ 1, 2 ]
```

Modifying `.length` truncates or extends the array.