String Literal 


In this article we will take a look at how to declare a string in JavaScript.  there is a different kind of way to declare a string in JavaScript specification ECMAScript 6. we also have new special characters. so will first take a look at what is the string?  how to declare a string literal? and how to create a multiline string literal in JavaScript.

What is the String?

The string is just a simple text data. you can also say the string is a collection of characters Numbers and symbols. In the JavaScript, string represents Unicode text. if you think then this will definitely come in your mind what is Unicode? Unicode text is a computing industrial standard For representing text data. Unicode store letter and characters By assigning a number of each one.

I will show you how to create a unique code in JavaScript after a few minutes.

Escaping

So you probably know that creating a string or to create a literal of string we need to specify single or double quotes. What if I want to add a single quote to specify apostrophe s in my string. When I use apostrophe s in the string it will terminate string literal.

Let’s take an example.

Show both literals are correct. because the string Set off with the single quote in the first example and double quote in the second example. What if I wanna add both at the same time.

So this statement produces an error message because at the same time we use the single and double quote for the string literal doesn’t know where to set off the string.

To solve this problem we have escaping. I will write down the previous example again without any error.

So this example doesn’t create an error message because we had used escaping here. The backslashes not only used for the quotation mark but also it can be used for non-printable Characters.

JavaScript special characters

  • \n:- Use for Newline
  • \r:- Use for carriage return
  • \t:- Use for Tabs
  • \’:- Use for Single Quate
  • \”:- Use for Double Quate
  • `:- Use for Backtick ( New in ECMAScript 6)
  • \$:- Use for Dollar Sign ( New in ECMAScript 6 )
  • \\uXXXX:- Use to represent Unicode Characters. Where 4x represent the hexadecimal value of the particular character.

Concatenation

Now we will take a look at how to add unique code in the string literal and also how to concatenate a string. let me show you how to create a simple concatenation with the string literal.

In ECMAScript 6 there is a one way to concatenate two strings using string template. Also known as string interpolation. So to do the same thing in ES6 instead of using double quote we are using backtick to concatenate the string.

So we will just edit the previous example literally.

I will just put the literal in the backtick. and to call variable in the literal use Dollar sign and open and closing curly braces. In that curly braces specify your variable name which you wanted to display in the string. And then specify your unique code.

As simple as that. You are free to choose your own way to write the string literal.

What if you wanted to print your string literal in the new line.

Suppose you have a string and the remaining string which you wanted to display on the new line what will you do?. So we have two methods to display string in the new line using special characters.

so we will just write.

so this statement will print string literal on two lines. on the first line, you will see one line and II will be on the second line.

The second method is the use of backtick to specify your string literal.

Using the backtick you will get the same result on the console. Using backtick you can create the multi-line string.

Now also you very interesting thing of string literals when you play with the string literal you will get the confusing result.

Then what is the result of the first and second statement? is it string or number? and what is the output?.

Let me know in the comment what is the output and type of these two statements. Write down your answer in the comment I will reply to you with the correct answer.

So that is all for this article we cover most of the string literals in this tutorial. We will see you in the next tutorial