Finding Out the Length of a String
var name = "Billy Smith"; console.log(name.length) // Returns 11
- Every character is counted – including spaces!
- If the string value is “”, its length will be 0
Reading a string’s length may seem trivial, but it’s very useful. For example, you might need to check that the user has entered only a short name and not a paragraph of text, or want to check whether the user has forgotten to enter their name at all.