Let's get started
var myString = "My name is Prashant"; var anotherString = 'I study in college';
In the above example, both
When we need to insert a single or double quote within a string, we escape that character by pre-pending it by
\ i.e. a backslash. For e.g.
var myString = "It is a \"double quoted\" string";
would result in
It is a "double quoted" string;
However, you may not need to escape the string if your surrounding quotes are not same as what you want in the string. Let me give you an example
var myString = 'I am a "double quoted" string';
would give the same result as above. As you may notice, here we didn't need to escape the quote as the surrounding quote is a single quote (
''), but what we used inside is a double quote.
Vice -versa would be true as well i.e. you may keep the double quotes for surrounding your string and use single quote inside of your string without escaping.
Apart from using escape sequence for multiple quotes within the same string, escape sequences are also used to type out characters which we may not be able to do otherwise. For e.g. a tab.
\'for single quotes
\"for double quotes
\\for backslash, when you want to use backslash as a character in a string
\rfor carriage return
\bfor word boundary
\ffor a form feed
We can concatenate two strings using the
+ operator. For e.g.
var myString = "My name is Prashant" + " and I love programming.";
would give result as
"My name is Prashant and I love programming".
Make sure you give spaces where you want. Concatenation doesn't add spaces by itself. You may notice, I've provided a space in the second part of string concatenation.
You can use the shorthand
+= for concatenation as well. For e.g.
var myString = "My name is Prashant"; myString += " and I love programming";
This would give the same result as above.
We can use variables to store part of strings and then use them for concatenation. For e.g. the above example can also be written as
myName = "Prashant"; myHobby = "programming"; myString = "My name is " + myName + " and I love " + myHobby;
To find the length of a string, we can make use of
length property available to
String data type as
var myString = "Prashant"; myString.length; // This would give us 8
Look carefully, how I have used the property using the
.(dot) with the variable.
You may directly use the string to access its length property instead of storing it to a variable like
"Prashant".length; // This would also give us 8
var myStr = "Example"; myStr; // This would give us the 1st character of myStr i.e. "E" myStr; // This would give use the 2nd character i.e. "x".
This was easy, wasn't it?
- Accessing the last character of a string
When you want to get the last character of the string, you may not know the last index of the string. In such cases, we can make use of the
length property, we just discussed above.
We know that
length property gives us the length of a string. So can you now think of at what index would the last character of a string be? Yes, it would be the
For e.g. in the above example,
Example has length
7 but the last index of this string is
6. I hope, now you get it.
myStr[myStr.length - 1]; // This would give you the last character of myStr
- Accessing last to Nth character in a string
In a similar fashion as above, if you want to get the nth character from the last, you can access it using
myStr.length - n, when
n is the nth character from the last.
var myStr = "Pan"; myStr = "C";
would result in the following error as we are trying to alter the contents of
TypeError: Cannot assign to read only property '0' of string 'Pan'
But this doesn't mean that we can't change the value of
myStr. You can always reassign it to any other value. It's just that individual characters of a string cannot be changed.