Why would the length of a sentence be useful to know?

Hi, i have done what the exercise has asked me to and passed it… But why is it relevant?.. like why would i want to know how many letters are in the sentence?

2 Likes

Seems like a reasonable question. I’ll try to give a reasonable answer. You might be hosting a message board, and have a character limit per post. This forum, for example has a minimum characters per post limit. As you get further along in learning to code, you’ll come to realize that you use the .length property of different objects quite often.

36 Likes

Can the length property only be used to determine the length of a string? Or can it also be used to determine the length of other data types?

It is a property of all objects, though some will never report anything but, undefined.

x = {a:1, b:2, c: 3, d: 4, e: 5}
{a: 1, b: 2, c: 3, d: 4, e: 5}
x.length
undefined

y = Array.from(Object.entries(x))
(5) [Array(2), Array(2), Array(2), Array(2), Array(2)]
0: (2) ["a", 1]
1: (2) ["b", 2]
2: (2) ["c", 3]
3: (2) ["d", 4]
4: (2) ["e", 5]
length: 5         //<<< note the physical property
__proto__: Array(0)
y.length
5
(y.length).length
undefined

Objects that have a length are generally iterable. Strings and arrays are iterable. Numbers, booleans, null, undefined, etc. are not.

4 Likes

3 posts were split to a new topic: As a programmer, you don’t know what data will the user input