String in JavaScript with examples. A string in JavaScript is an object which is an order of zero or more numbers. A string is both enclosed in the single quotation or double quotation as ( ‘ ) and ( “) individually, for example, “String In Javascript” or ‘String In Javascript’ both are pleasant and the same. The string can hold characters, numbers, symbols, and even special characters like ‘\t’, ‘\b’, ‘\n’,etc. Some of the examples for string are “Name”, “Country”, “1234”, “Capital” + “world”, etc. In javaScript string can be performed in two ways, one way is as a string object “using the new keyword” and another way is a string literal.
String in JavaScript as a string literal 🙂
The String can be defined or stored as a string literal, without using the new keyword. The syntax of this is given below.
var str = "Hello, SoftCodeON" ;
Let’s understand with an example of creating a string in JavaScript as a string literal.
Example:)
<p id="string"></p> <script> var x = "Learn Javascript Strings."; document.getElementById("string").innerHTML = x; </script>
String in JavaScript as string object “using new keyword:)
The String can be represented or stored as an object of the String class, the string object is performed by using the new keyword. The syntax of this is given below :)(:
var str = new String( "Hello, SoftCodeON" );
as above syntax the new keyword is used to create an object of string.
Example:)
<h2 id="string"></h2><br> <script> var str = new String("Learn Javascript Strings."); document.getElementById("string").innerHTML = str; </script>
Conclusion
It is one of the major objects or literal which describe the string value. The string is an order of zero or more characters. A string is both enclosed in a single quotation or a double quotation. The string can initialize two ways, one as an object of string and another way as a string literal.