JavaScript Tutorial

Define JavaScript

JS Arithmetic Exercise

JavaScript Comment

JavaScript Variable

JavaScript Operators

Conditional Statements

Logical Operators

JavaScript Slider

JS Login Form

JS Popup Boxes

JavaScript Loops

JavaScript Arrays

JS Mouse Events

JS Keyboard Events

Styling Elements

External JavaScript

JS Noscript Tag

JS String Functions

JS Math Functions

JS Date Functions

JS Theme Changer

JS Assignment

Page Stats

Visitor: 91

JavaScript String Methods

A function is a set of statements that performs a specific task within a program and returns a result. A function may also accept arguments.

A function tells an application what and how to perform a specific task when an event is generated.

A String is a collection of characters, a String can contain Small letters(a-z), Capital letters(A-Z), Digits(0-9), Special Characters (&, #, @) etc. A String can be any text inside quotes. In JavaScript, we can use 'single' or "double" quotes to define a string.

String functions are those functions which perform the desired action on the characters. Common String Functions/Methods are:

MethodsDescription
lengthlength is a JavaScript property, use to return the length of strings.
charAt()Returns the first character at the specified index (position)
concat()Joins two or more strings, and returns a copy of the joined strings
indexOf()Returns the position of the first found occurrence of a specified value in a string
match()Searches a string for a match against a regular expression, and returns the matches
replace()Searches a string for a value and returns a new string with the value replaced
search()Searches a string for a value and returns the position of the match
slice()Extracts a part of a string and returns the extracted part in a new string.
substr()Extracts a part of a string from a start position through a number of characters
substring()Extracts a part of a string between two specified positions
toLowerCase()Converts a string to lowercase letters
toUpperCase()Converts a string to uppercase letters
trim()Removes whitespace from both ends of a string

Example 1: String length property



Example 2: charAt function, Check input name is boy or girl.



Example 3: String concat function.



String IndexOf

var str = "Javascript is easy and Javascript is interesting";
var pos = str.indexOf("javascript");

var str = "Javascript is easy and Javascript is interesting";
var pos = str.lastIndexOf("javascript");

String Replace
str = "I like Programming";
var n = str.replace("Programming","Scripting");

String Slice
var str = "Apple, Orange, Mango";
var res = str.slice(7,13);
var res = str.slice(7);

String Substring
var str = "Apple, Orange, Mango";
var res = str.substring(7,13);

String Lower Case
var text1 = "Hello World!"; // String
var text2 = text1.toLowerCase(); // text2 is text1 converted to upper

String Upper Case
var text1 = "Hello World!"; // String
var text2 = text1.toUpperCase(); // text2 is text1 converted to upper