JavaScript – Strings


What is a String ?

A String is considered as the sequence of characters combined to form a String.

It may be numerals, letters, or symbols.

What is a String in Javascript ?

A String is an object that represents a sequence of characters.

Why is String used in javascript ?

Strings are used for storing and manipulating text.

String may be zero or more characters, should be written inside quotes.

How to construct a String in javascript ?

There are two ways to construct String listed below,

1. Using string literal

2. Using string object (with new keyword)

Javascript interview questions basic !!!

Click to Learn More about – Javascript online learning

1. Using String Literal

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = "My Name is Abdul Kalaam";
			var b = "My Name is Mohandas Karamchand Gandhi";
			var c = "My Name is Jawaharlal Nehru";
			var d = "My Name is Bhimrao Ambedkar.";
			var e = "My Name is Sachin Tendulkar";
			var f = "My Name is Virendar Shewag";
			var g = "My Name is Rahul Dravid";
			document.write(a+"<br/>");
			document.write(b+"<br/>");
			document.write(c+"<br/>");
			document.write(d+"<br/>");
			document.write(e+"<br/>");
			document.write(f+"<br/>");
			document.write(g+"<br/>");
		
		</script>
	</head>
	<body></body>
</html>

Output :


My Name is Abdul Kalaam

My Name is Mohandas Karamchand Gandhi

My Name is Jawaharlal Nehru

My Name is Bhimrao Ambedkar.

My Name is Sachin Tendulkar

My Name is Virendar Shewag

My Name is Rahul Dravid

2. Using string object (with new keyword)

This can be done by using “new” keyword.

Example :


<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = new String("My Name is Abdul Kalaam");
			var b = new String("My Name is Mohandas Karamchand Gandhi");
			var c = new String("My Name is Jawaharlal Nehru");
			var d = new String("My Name is Bhimrao Ambedkar.");
			var e = new String("My Name is Sachin Tendulkar");
			var f = new String("My Name is Virendar Shewag");
			var g = new String("My Name is Rahul Dravid");
			document.write(a+"<br/>");
			document.write(b+"<br/>");
			document.write(c+"<br/>");
			document.write(d+"<br/>");
			document.write(e+"<br/>");
			document.write(f+"<br/>");
			document.write(g+"<br/>");
		
		</script>
	</head>
	<body></body>
</html>

Output :

My Name is Abdul Kalaam

My Name is Mohandas Karamchand Gandhi

My Name is Jawaharlal Nehru

My Name is Bhimrao Ambedkar.

My Name is Sachin Tendulkar

My Name is Virendar Shewag

My Name is Rahul Dravid

Javascript has a lot of String methods, some of them are listed below.

SnoMethodDescription
1charAt()This charAt() method provides the char value present at the specified index.
2charCodeAt()This charCodeAt() method provides the Unicode value of a character present at the specified index.
3concat()This concat() method provides a combination of two or more strings.
4indexOf()This indexOf() method provides the position of a char value present in the given string.
5lastIndexOf()This lastIndexOf() method provides the position of a char value present in the given string by searching a character from the last position.
6search()This search() method searches for a specified regular expression in a given string and returns its position if a match occurs.
7match()This match() method searches for a specified regular expression in a given string and returns that regular expression if a match occurs.
8replace()This replace() method replaces a given string with the specified replacement.
9substr()This substr() method fetches the part of the given string on the basis of the specified starting position and length.
10substring()This substring() method fetches the part of the given string based on the specified index.
11slice()This slice() method fetches the part of the given string. It allows us to assign positive as well negative index.
12toLowerCase()This toLowerCase() method converts the given string into lowercase letter.
13toLocaleLowerCase()This toLocaleLowerCase() method converts the given string into lowercase letter on the basis of hosts current locale.
14toUpperCase()This toUpperCase() method converts the given string into uppercase letter.
15toLocaleUpperCase()This toLocaleUpperCase() method converts the given string into uppercase letter on the basis of host?s current locale.
16toString()This toString() method provides a string representing the particular object.
17valueOf()This valueOf() method provides the primitive value of string object.