Coding / Programming Videos

Post your favorite coding videos and share them with others!

Javascript String Length: How to determine the size of a string

In this tutorial, we’ll take a look at the JavaScript String Length property and how you can use this to determine the size of a string.

It’s a very common thing to do but to use the JavaScript string length property is luckily really simple.

In the tutorial, you’ll learn how to use the .length property to find out the length of a string but we’ll also look at some common scenarios that you might want to use the length property for.

Checking for null / falsy JavaScript string lengths – This is something that is quite common, you might have an array of strings that you want to check whether or not they are empty i.e. remove any blank strings from an array. Whilst a blank string is considered falsy, you might want to check the additional length property to make your code a bit more readable (or if you have a specific case to allow null or undefined values).

Checking variables with a JavaScript string length comparison – If you have two strings that you want to compare the length of then you’ll need compare their lengths by accessing the length property of both variables.

The Javascript string length undefined Problem – If you get an undefined result when trying to get a string’s length in JavaScript then chances are you don’t actually have a string. In the tutorial i’ll talk you through how to check that you’re actually working with a string and to convert it to one if necessary.

Javascript String Length Validation on Input fields – The final example we’ll look at is how to validate an input field’s length with JavaScript by checking the length property of the string entered into the input box.


Leave a Reply

Please Login to comment
Notify of
Translate »