Google News
logo
Javascript Strings
String is the built-in object corresponding to the primitive string data type. It contains a very large number of methods for string manipulation and examination.

Strings are values made up of text and can contain letters, numbers, symbols, punctuation, and etc,. Strings are contained within a pair of either single quotation marks '   ' or double quotation marks "   ".

There are 4 ways to create string in JavaScript
1. String literal
2. String concatenation
3. String As Array
4. String object (using new keyword)
String Literal :
<html>
<head>
<title>String Literal</title>
</head>
<body>
 
<p id="text-1"></p>
<p id="text-2"></p>
 
<script type="text/javascript">
 
  var string_1 = "www.freetimelearning.com";
  var string_2 = 'www.freetimelearn.com';
  document.getElementById("text-1").innerHTML = string_1;
  document.getElementById("text-2").innerHTML = string_2;
 
</script>
 
</body>
</html>
Output :

www.freetimelearning.com

www.freetimelearn.com

String Concatenation :
<html>
<head>
<title>String Concatenation</title>
</head>
<body>
 
<p id="concat"></p>
 
<script type="text/javascript">
 
  var my_site = 'Welcome ' + "to" +  'www.freetimelearning.com';
 
  document.getElementById("concat").innerHTML = my_site;
 
</script>
 
</body>
</html>
Output :

Welcome to www.freetimelearning.com

String As Array :
<html>
<head>
<title>String As Array</title>
</head>
<body>
 
<p id="txt_1"></p>
<p id="txt_2"></p>
<p id="txt_3"></p>
<p id="txt_4"></p>
<h4 id="txt_5"></h4>
 
<script type="text/javascript">

  var text = 'Java Script';
  document.getElementById("txt_1").innerHTML = text[0];
  document.getElementById("txt_2").innerHTML = text[1];
  document.getElementById("txt_3").innerHTML = text[2];
  document.getElementById("txt_4").innerHTML = text[3];
  document.getElementById("txt_5").innerHTML = text.length

</script>
 
</body>
</html>
Output :

J

a

v

a

11

String Object :
<html>
<head>
<title>String Object</title>
</head>
<body>
 
<p id="obj_1"></p>
<p id="obj_2"></p>
 
<script type="text/javascript">

  var site_1 = new String();
  site_1 = 'www.freetimelearning.com';
  // Another Formate
  var site_2 = new String('www.freetimelearning.com');
 
  document.getElementById("obj_1").innerHTML = site_1;
  document.getElementById("obj_2").innerHTML = site_2;

</script>
 
</body>
</html>
Output :

www.freetimelearning.com

www.freetimelearning.com