• It is used to storing single characters, and
• It stores an array of characters
• It is used to manipulating text – search a particular letter and replace text
Note
It is immutable, the string cannot be changed in JavaScript. It is impossible to change a particular character.
Creation of string
• By string literal
var string-name = “string value”;
zero or more characters written inside single or double quotes or backticks. The string indexes are zero-based, the first character is in position 0 and the second in 1.
• By string object using ‘new’ keyword
var string name = new string (“string value”);
In this, a new keyword is used to create an instance of a string. The string object has some disadvantages, for example, the execution speed is slow.
String literals
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta charset='utf-8' />
<title></title>
</head>
<body>
<h2>String </h2>
<script type='text/javascript'>
var name = ' Welcome Ravi, '; //literal
var dept = 'MCA'
document.write(name);
document.write('He is ' + dept + ' department');
</script>
</body>
</html>
String object
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta charset='utf-8' />
<title></title>
</head>
<body>
<h2>String </h2>
<script type='text/javascript'>
var name = new String(' Welcome Ravi, '); //literal
var dept = 'MCA'
document.write(name);
document.write('He is ' + dept + ' department');
</script>
</body>
</html>