Skip to content
the poor coder

javascript length

Poor Man
Published
Reading time
2 min read
On this page

Javascript Length Explained#

As a web developer, I have had a lot of experience working with Javascript. One of the most commonly used properties of arrays and strings in Javascript is the length property. In simple terms, the length property returns the number of items in an array or the number of characters in a string.

Using the length property with Arrays#

Let's say we have an array of numbers:

Text

const numbers = [1, 2, 3, 4, 5];
console.log(numbers.length); // Output: 5

In the above example, we have an array called numbers which contains 5 elements. We can use the length property to find out how many elements are in the array. The output will be 5.

Using the length property with Strings#

We can also use the length property with strings:

Text

const str = 'Hello World';
console.log(str.length); // Output: 11

In the above example, we have a string called str which contains 11 characters. We can use the length property to find out how many characters are in the string. The output will be 11.

Multiple Ways to Use the length Property#

In addition to using the length property with arrays and strings, there are other ways we can use it as well. For example, we can use it in a loop to iterate over an array:

Text

const colors = ['red', 'green', 'blue'];

for(let i = 0; i < colors.length; i++) {
  console.log(colors[i]);
}

In the above example, we have an array called colors which contains 3 elements. We use a for loop to iterate over the array and print each element to the console.

Another way we can use the length property is to resize an array:

Text

let numbers = [1, 2, 3];
console.log(numbers.length); // Output: 3

numbers.length = 5;
console.log(numbers); // Output: [1, 2, 3, undefined, undefined]

In the above example, we have an array called numbers which contains 3 elements. We set the length property of the array to 5, which adds two undefined values to the end of the array.

Conclusion#

The length property is an important part of working with arrays and strings in Javascript. It allows us to easily determine how many elements are in an array or how many characters are in a string. We can also use it in loops to iterate over arrays and we can resize arrays using the length property.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC