JavaScript String Methods


String methods help you to work with strings.


String Methods and Properties

Primitive values, like "John Doe", cannot have properties or methods (because they are not objects).

But with JavaScript, methods and properties are also available to primitive values, because JavaScript treats primitive values as objects when executing methods and properties.


String Length

The length property returns the length of a string:


Example

var txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

var sln = txt.length;


Finding a String in a String

The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:


Example

var str = "Please locate where 'locate' occurs!";

var pos = str.indexOf("locate");

Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya