<script> var name1 = "Rohan Singh"; var name2 = 'Reet Gunia'; var name3 = `Jay jio` var name4 ="" </script>
In the above example, all
name4 are strings. Although the
name4 variable is an empty string, still, it is a string data type.
Everything between the single or double quote surrounding the value will be treated as a single character except the matching quote and escape characters(discuss further in this tutorial).
We can have the quotes as a character, but they should not match the quote surrounding the string value.
<script> var statement1 = "Let's go for a walk"; //valid var statement2 = 'He said "Give me the pan"'; // valid var statement3 = "He said "Give me the pan""; // error </script>
Behind the code
In the above code
statement2 variables are valid because they have different quotes for surrounding and different quotes for characters.
statement3 the surrounding quotes and character quotes match that result in error.
So how can we use single and double quotes in the same string?
There are two ways to use single and double quotes in the same string, we can either use the escape character or backtick string.
Here is the example of a backtick string using both single and double quotes inside the string.
<script> var statement = `He said "Let's go"`; </script>
There is no difference between single-quoted and double-quoted strings, but a backtick quoted-string provides more features as compared to a single and double-quoted string.
We will discuss the backtick string in detail in upcoming tutorials.
And like the properties of an object, the string contains a built-in property called
length that returns the total number of characters present in the string.
<script> var statement = "123456abcdef" var len = statement.length; document.write("The Length of the statement is: "+len); </script>
The Length of the statement is: 12
String Escape characters
Whenever the JS engine reads the \ symbol inside the string it does not treat the \ symbol as a normal character it treats it as a special escape character.
In the above examples where we used the string surrounding quote inside the string, it threw an error, so in order to use both single and double quotes inside the string, we used backticks string.
But that problem can also be solved using escape characters and for most of the cases, you will be seeing that all the developers prefer to use escape characters rather than backticks string.
var statement = "He said "Let's Go""; //error
Solve using the escape character
var statement = "He said \"Let's Go\"";
"character into a normal character.
<script> var statement = "This is \ a backslash "; document.write(statement) </script>
This is a backslash
From the output, you can see that the JS engine does not read the
\ symbol inside the string.
If you want to write the
\ symbol in the string you have to escape it by using another
\ character, that will tell the engine to treat the
\ symbol as a normal character.
<script> var statement = "This is \\ a backslash "; document.write(statement) </script>
This is \ a backslash
<script> var statement = "First Line \nSecond Line \nThird line"; console.log(statement) </script>
First Line Second Line Third line
String Character Access
The string stores all the characters in sequential order, so we can treat the string as an array-like object and grab the individual characters using its numerical index value.
The index value of every non-empty string starts with 0 up to n-1 where n is the total number of characters present in the string.
<script> var data = "This is a String"; console.log(data); //first charecter console.log(data); //second charecter </script>
We can compare two string values. And the comparison test between two strings is based on their value and lexical order.
<script> var str1 ="Hello"; var str2 = "Tech"; console.log(str1 == str2) //false console.log(str1 > str2); //false console.log(str1 < str2); //true </script>
Acoording to the lexical order 0-9 < a-z < A-Z.
var name = "Rahul"; //primitive string
var name = new String("Rahul"); //object string
The difference between both the strings can be seen through identify operator === and eval() statement.
<script> var str1 ="3+3"; // primitive string var str2 = new String("3+3"); //object string console.log(typeof str1); //string console.log(typeof str2); //object console.log(str1 == str2); //true (check for value equal) console.log(str1 === str2); //false (check for value as well as type equality) console.log(eval(str1)); //6 console.log(eval(str2)); //string 3+3 </script>
string object true false 6 "3+3"
- A string is a collection of characters.
- String stores all the characters in sequential order.
- \ treated as a special character in String, and it is known as an escape character.
- An escape sequence is a sequence of characters that translated into a special meaning in the string.