length

Category:Variables

[string].length

Category: Variables

Returns the length of the string.

When doing string parsing or manipulation you usually need to know how long a string is. length is actually not a function, but an attribute of a string object. You need to use a variable containing a string, followed by a dot ("."), followed by the attribute name length.

Examples


var word="supercalifragilisticexpialidocious";
console.log(word.length);

Example: First and Last See if the first letter is the same as the last letter in a word.

// See if the first letter is the same as the last letter in a word.
var word="racecar";
var first=word.substring(0,1);
var last=word.substring(word.length-1,word.length);
console.log(first == last);

Example: Palindrome Check if a word is a palindrome.

// Check if a word is a palindrome.
var word="racecar";
while(word.length>1 && word.substring(0,1)==word.substring(word.length-1,word.length)) {
  word=word.substring(1,word.length-1);
}
if(word.length==0 || word.length==1) console.log("palindrome");
else console.log("not palindrome");

Syntax

[string].length

Parameters

Name Type Required? Description
string string Yes The string to find the length of.

Returns

The length of the string

Tips

  • length is always >= 0

Found a bug in the documentation? Let us know at documentation@code.org

Found a bug in the documentation? Let us know at documentation@code.org