Work With Strings in JavaScript II

JavaScript·2 min read·Jan 1, 2026

In JavaScript, string primitives declared as string literals using single quotes '' and double quotes "" are automatically wrapped in the String object, allowing you to use object-specific properties and methods.

Getting the length of strings

To get the number of characters of a string (its length), you can use its length property:

string.length

Example

Let's consider this script, that filters an array based on the length of its string values:

const fruits = [  'banana',  'apple',  'pineapple',  'raspberry',  'orange',  'lemon'];console.log(fruits.filter(fruit => fruit.length <= 5));

When executed, it will use the filter() method of the fruits array to filter out the strings whose length property is greater than or equal to 5 characters.