String in Javascript
Whenever we need to have some string (text) data in our program, we can make use of strings. Basically, the string is just a set of characters. We are going to have a look at String in Javascript, and also have a look at some string methods over here. But, the question is that how are we going to create a string? Well, we are going to use single quotes or double quotes.
String in Javascript
So, creating a String in Javascript is as simple as the below instruction –
var stringvar = ‘Hello from gyanipandit’; // you can also use the double quotes here.
You can try doing console.log(stringvar), and you get the string onto the console.
Have a look at the below program, which tries to demonstrate the same thing, creating the String in Javascript –
If you try to run the above program, into the console, the string ‘Welcome to GyaniPandit’ is going to be the output.
As you can see in the output, the string is there. You can also use the double quotes there. Also, we can make use of the backticks (these are found just above the tab key, or below the escape key) here. Let’s try this once as well.
If you try running the above program, the output is again simply the string. (The same as the prior outputs in this case), and you can also check the type of the data that variable has, with the help of the type of operator, and you will find that it is the String in Javascript.
We are going to explore the strings a lot, and we are going to also have a look at some different methods that can be of great help to us, at times. Now, let’s get ahead and explore the strings in Javascript.
If you want to have quotes inside the String in Javascript
As you know already, we can make use of single or double quotes for the strings. This means that if we are using the single quotes, we can simply say that the string is going to be surrounded by the single quotes, and the same is in the case of double quotes(it would be surrounded by double quotes). But sometimes, we might encounter such situations, when we need to use the single or double quotes inside the string, which need to be the part of the string here. So, how we can do this? The answer is quite easy, and this involves either the use of an escape sequence or using the appropriate quotes for the strings.
If you want to use quotes inside a string, then you can either use the escape characters, or there is one more technique to wrap the string with inappropriate quotes. What I mean is that if you want to use single quotes in a string, then wrap the string in double quotes. If you want to use double quotes in a string, then wrap that string into single quotes. Here are some examples for you to understand.
As you can see, we are simply using the appropriate quotes in the above program, to wrap the string. This is like if we want the double quotes in the String in Javascript, we use the single quotes for the string or vice versa. Have a look at the resulting output →
You can also access the characters of the string with the help of the index. If you try somestring[5] in the above program, you will get the character present at the 5th index, which is the 6th element (since the index starts from zero). We can also use the charAt method for doing this thing(we are going to explore the charAt method ahead)
Now, that we have understood how can we create String in Javascript, and also some other things, let’s now concentrate on some methods related to the String in Javascript, which help us do a lot of different things, like calculating the length of the string (basically the number of characters), converting the string to lowercase or uppercase, and much more.