Syntax
str.length;
Return Value
Returns the number of characters in the string
Example
"programmer".length;
==> 10
Syntax
str.toUpperCase();
Return Value
Returns a copy of the string with all characters converted to uppercase
Example
"wdi-30 is great".toUpperCase();
==> "WDI-30 IS GREAT"
Syntax
str.toLowerCase();
Return Value
Returns a copy of the string with all characters converted to lowercase
Example
"SHHH, I'M WHISPERING".toLowerCase();
==> "shhh, i'm whispering"
Syntax
str.indexOf(searchString[, fromIndex]);
Return Value
Returns the index of the first occurrence of the specified value. Returns -1 if the value isn't found.
Example
"Programming is fun".indexOf("m");
==> 6
"Programming is fun".indexOf("z");
==> -1
"Programming is my favorite thing".indexOf("m", 10);
==> 15
//(starts searching left-to-right from index 10)
Syntax
str.lastIndexOf(searchString[, fromIndex]);
Return Value
Returns the index of the last occurrence of the specified value. Returns -1 if the value isn't found.
Example
"racecar".lastIndexOf("a");
==> 5
"racecar".lastIndexOf("a", 5);
==> 1
//(starts at index 5 and searches right-to-left)
Syntax
str.startsWith(searchString[, fromIndex]);
Return Value
Returns a boolean indicating whether the search string was found
Example
"Programming is the best".startsWith("Pro");
==> true
"Programming is the best".startsWith("pro");
==> false
"Programming is the best".startsWith("Pro", 0);
==> true
"Programming is the best".startsWith("Pro", 1);
==> false
//(starts checking from the specified index)
Syntax
str.endsWith(searchString[, fromIndex]);
Return Value
Returns a boolean indicating whether the search string was found
Example
"Programming is the best".endsWith("best");
==> true
Syntax
str.includes(searchString[, fromIndex]);
Return Value
Returns a boolean indicating whether the search string was found
Example
"She sells sea shells".includes("sea");
==> true
"She sells sea shells".includes("sea", 13);
==> false
// (starts searching left-to-right from index 13)
Syntax
str.slice(startIndex[, endIndex]);
Return Value
Returns a new string with the characters from the start index to the end index-1 (not inclusive)
Example
"supercalifragilisticexpialidocious".slice(20);
==> "expialidocious"
"supercalifragilisticexpialidocious".slice(20, 22);
==> "ex"
Syntax
str.substr(startIndex[, length]);
Return Value
Returns a new string from the start index of the specified length (if given)
Example
"burrito supreme".substr(5);
==> "to supreme"
"burrito supreme".substr(5, 6);
==> "to sup"
Syntax
str.substring(startIndex[, endIndex]);
Return Value
Returns the characters from start index up to but not including the end index (if specified)
Example
"steak tacos".substring(5);
==> " tacos"
"steak tacos".substring(5, 6);
==> " "
Syntax
str.trim();
str.trimLeft();
str.trimRight();
Return Value
Returns a new string with leading and trailing whitespace removed (just leading whitespace for .trimLeft(), just trailing whitespace for .trimRight())
Example
" so much space ".trim();
==> "so much space"
" so much space ".trimLeft();
==> "so much space "
" so much space ".trimRight();
==> " so much space"