[string].length
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
Example: how long?!
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 | The string to find the length of. |
Returns
Tips
- length is always >= 0
Found a bug in the documentation? Let us know at support@code.org.