Skip to Content

Advertisement

You (Level 0)
0% Complete
Last Badge Earned
None

The String is one the most used objects in JavaScript, which is why it is so important to know its properties and methods. To clear up some confusion, you already know this object as a string variable. As I just explained in the previous tutorial, this variable was created by a string() function. So, this variable is actually an object with properties and methods.

The string is a pretty large class because developers want to do some many different things with strings. The idea of a string in itself is very complicated. You might want to know how many "e" characters are in it, but you might also want to know the total number of characters in the string. Those are just two common reasons why we have properties. JavaScript does a really good job of providing us with a wealth of properties to get what we need from our strings.

Common String Properties

constructor - tells us the function that created the String object's prototype

Example var example = "Meerkat";
document.write(example.constructor);
Result
function String() { [native code] }

length - returns the total number of characters in a string

Example var example = "Meerkat";
document.write(example.length);
Result
7

Common String Methods

A string is a very changeable variable, which means you need to be careful when using string methods. Many of the string methods will manipulate the initial value of the string, which cannot be recovered. You might have noticed already, but methods are easily distinguished by the fact they have () following them.

charAt() - returns character using its index

Example var example = "Meerkat";
document.write(example.charAt(0));
Result
M

indexOf() - returns the index of the first instance of a specified value

Example var example = "Meerkat";
document.write(example.indexOf("e"));
Result
1

lastIndexOf() - returns the index of the last instance of a specified value

Example var example = "Meerkat";
document.write(example.lastIndexOf("e"));
Result
2

match() - finds all matches in the string of a specified value

Example var example = "Meerkat meerkat";
document.write(example.match(/m/gi)); // Read about Regexp to understand this
Result
M,m

replace() - finds all matches in the string of a specified value and then replaces them with a new substring

Example var example = "Meerkat";
document.write(example.replace("ee","oo"));
Result
Moorkat

search() - looks for a match in the string with a specified substring and returns the substrings index in the string

Example var example = "Meerkat";
document.write(example.search("a"));
Result
5

slice() - extracts and returns part of the string

Example var example = "Meerkat";
document.write(example.slice(4,7));
Result
kat

split() - splits string into an array of substrings

Example var example = "Meerkat";
document.write(example.split(""));
Result
M,e,e,r,k,a,t

substr() - extracts characters from a string starting at a specified position and ending at a specified length from the starting position

Example var example = "Meerkat";
document.write(example.substr(2,3));
Result
erk

substring() - extracts characters from a string starting between two specified positions

Example var example = "Meerkat";
document.write(example.substring(3,4));
Result
r

toLowerCase() - converts all letters in string into lowercase

Example var example = "Meerkat";
document.write(example.toLowerCase());
Result
meerkat

toUpperCase() - converts all letters in string into uppercase

Example var example = "Meerkat";
document.write(example.toUpperCase());
Result
MEERKAT

I've attached my favorite JavaScript and jQuery book. I would encourage you to read it as well.

Your code will execute in this window.



Let's explore some more tutorials or topics!

Next Tutorial Previous Tutorial

Advertisement

If you enjoyed this resource, support me by sharing this page with others.

Comment on

Show Comments

No comments yet... Be the first!



Advertisement