Working With Strings in JavaScript II
JavaScript·3 min read·Mar 14, 2025
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.lengthExample
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.
Unlock the Build CLI Apps in JavaScript & Node.js module
Learn how to build, integrate, and safeguard scalable CLI tools, scripts, and apps with JavaScript, Node.js, npm, and Git.
You get immediate access to:
- 45 focused lessons across JavaScript, Node.js, npm, and Git
- 17 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle