JavaScript String Length property explained
Tags: javascript, tutorial, beginners
Published: 2021-10-18 12:06:19

In this short tutorial, we learn about the JavaScript string length properly. We also look at how JavaScript finds the length of a particular string.

This tutorial is a part of our initiative at [Flexiple](https://flexiple.com/), to write short curated tutorials around often used or interesting concepts.

### Table of Contents - JavaScript startsWith():
- [JavaScript string length property](#javascript-string-length-property)
- [How do you find the length of a string in JavaScript?](#how-do-you-find-the-length-of-a-string-in-javascript)
- [Closing thoughts](#closing-thoughts)

## JavaScript String Length Property:
The JavaScript string property is used to return the length of a string in JavaScript. Although this property is commonly used with other JavaScript methods it is important to remember that length in itself is not a method.

### How does JavaScript return the length of a string?
JavaScript does not return the length but rather returns the code units occupied by the string. It uses the UTF-16 string formatting methods to store characters. This essentially means that the characters in your string are encoded into a 16-bit long binary number before being stored.

So whenever the .length property is involved, JavaScript looks up and returns the number of code units occupied by the string. This is why JavaScript returns 2 when the length property is called upon by certain characters such as emojis, etc. It does this because these characters take up 2 code units.

## How do you find the length of a string in JavaScript?
The length of a string in JavaScript can be found using the .length property. Since .length is a property it must be called through an instance of a string class.

### Syntax:
The syntax to use the length property is as follows:
```javascript
string.length
```
Here, “string” refers to the string whose length you are looking to return.

### Code and Explanation:
```javascript
let str = 'Join our community of freelance developers';

console.log(str.length);
```
This is a common problem while using a switch case, this can be solved by using new blocks for each case. However, this is not a problem while using the var keyword. The below code snippet should give an idea of the case:
```javascript
{
  let num = 2; 
  console.log(num) // 2
}
{
  let num = 4; 
  console.log(num)// 4
}
```
The above code snippet returns the following:
```javascript
>42
```
The length of the string is 42.

Earlier we had methods through which the length property would return a higher number in the case of a few characters. We have an example of the same below.

Here we have involved the length property upon a string containing an emoji:
```javascript
let emoji = '😀'; console.log(emoji.length);
```
The above code snippet returns the following:
```javascript
>2
```
As you can see it returns 2, this is because this emoji takes up 2 code units.

## Closing thoughts:
Once you are done practicing I would recommend you look up other string properties. For the few of you who are curious how UTF-16 works you can use this link to read more.

Apart from this, in case you haven’t already figured it out - The JavaScript string length would return 0 when invoked on an empty string.