JavaScript String Methods

Published on: September 3, 2016

Welcome to the eleventh Easy JavaScript Tutorial, Beginner JavaScript Tutorial, part of Easy Programming! This tutorial covers Strings! 

As I mentioned in the first video, everything in JavaScript is an object, or they're treated like objects even if you think they're not. And as in real life you can do things to objects, in JavaScript, you can do things with its objects. 

Objects have two main pieces, properties and methods. If we have a book, a property of that book would be the title, page number, etc. In a string, the property we're most interested in is the length property, which will tell us the length of the string. There is also the constructor and prototype, something we'll dive into later. 

A method is like a function. If you have a car, a function, or method of this car would be to drive, or park, turn on windshield wipers. 

To fork the fiddle and follow along: https://jsfiddle.net/easyjs/a1xvg2jm/

Let's look at the various String Methods available to us:

Method Description
length It's technically a property, and it tells you the exact number of characters in a given string
indexOf() first occurance of a specified text
search() search for text and returns position
slice() extract part of a string - takes beginning and end positions
substring() like slice, extract part of a string, only start position is required
substr() extract part of a string, first parameter is the start position, and second is the length
replace() replace text in a string
toUpperCase() convert to upper case
toLowerCase() convert to lower case
concat() concatenate two strings
charAt() returns character at specified position

Remember to checkout the Resources section below for associated downloadable content, JSFiddle links, and other resources. Watch the video and follow along!

Resources:



Comments: