JavaScript Glossary - Lesson #24

JavaScript String .length Method

The length property of the String object returns the number of code points in a string object. It indicates the length of the string.

js
""scotch.io"".length
// 9
js
// New string
const word = "accessibility"

// Find the length of the word
let lengthOfWord = word.length

// Display length
console.log(`${word} has ${lengthOfWord} letters in it`) // accessibility has 13 letters in it
js
const stringLength = string.length

The length property returns the number of code points in a given string. When called on an empty string, it returns 0.

The length property in conjunction with an if statement is used to verify the length of a password entry and returns a message if the length condition is satisfied or otherwise.

js
// Create new password
const password = 'p@ssword';

// Verify password length
if (password.length < 8) {
  console.log('Your password is weak')
} else {
  console.log("Strong password selected")
} 

// output: Strong password selected

Want to improve your coding and design skills?

I'm continually researching the best practices and tools for coding.
Join 50,000+ developers looking to make cool stuff.

We value your privacy. 1-click unsubscribe.

Chris Sev

Chris Sev

Chris Sev is the co-founder of Better Dev. Coding better every day. Previously he created Scotch.io which was acquired.

Comments

What did you think of the article? Let us know!
(these comments are powered by GitHub issues and use 0 trackers)