Lesson 2 String Property In Javascript

In this lesson, we are going to learn more about string type in JavaScript, and then we are going to give a short overview about more primitive data types like Boolean and null/ undefined data type.
We have a few objectives here:


Introduce the String Concatenation. Work with String Escape character and string length property Give Overview about the Boolean, null and undefined type.

String Type

In the previous lesson we have introduced string type and here we have discussed more about this category. So in JavaScript you can write string in three-way, you can write string in single quotation (‘string Character ’), you can write in double quotation (“String Character”) or you can write in backticks (String Character) and you can also write all three style of string in the same script file too. Here is an example of string below where we have used all three styles of string. img1

You can see in the above output, no matter what quotation I am using but the output written in the double quotation, it is because JavaScript treats single quote and backticks like a same way with the double quote. NOTE: If we start writing with double quotation but end it with single or Backticks Quotation, then there is an error.
Because quotes need to match.

In JavaScript, it is valid to use single quotation inside of the double quotes.
But if we write single quote at the starting point and inside the quote and then write again single quote at ending point (like this : ‘ It’s JavaScript’) then it will give an error.

You can see that JavaScript thinks the strings end next to ‘it quotation’ but in reality I wanted it to end next to ‘Example quotation’.
Concatenation: So one of the things that we can do with strings is added them together just like numbers.
Like we can add “string” + “string”.
JavaScript will combine them into one string.
It is called Concatenation.
Here is an example of concatenation below:

Let’s see what happens if we add space after “Hey ” string.

You can notice space between ‘Hey’ and ‘geeks’ in above output screenshot, so this is how we can use space between texts in string concatenation.

Escape Character

Sometimes we want to have double quotation inside of double quotation string like something similar to this string text ("This is "Escape Character" example"), then it will give syntax error unexpected identifier, it is because we can’t use double quote inside double quote and JavaScript treat both as a different string. img6

To solve this Problem we use a JavaScript escape character which starts and end with the backslash “\”.

So backslash and then followed by a double quote is how we tell JavaScript that we want a double quotation in the string. Note: If we write without the backslash, it will give an error.

Length property

Every string we create has a length property.
Length property refers to the number of characters in the string.
So if I create a string “Geeks” and add .length with it, it tells me that it is 5 characters long.

It includes each and every character like words, numbers, symbols, and spacing as well. img9

Access individual character by using [ ] We can also retrieve the entire length of a string using length property and we can also retrieve individual characters like the 4th character or 7th character by using this “square bracket [ ]” notation.
This function mainly used with variables.

It works when we put [ ] and gives a number to it, that number will correspond with the position with the string of that character we want.

NOTES: If we write "Hellow Geeks"[0] and hit enter, then JavaScript starts keeping track with [0] and it will give us “H” In this lesson, we have discussed more about strings in JavaScript in detail.
We have also discussed single, double and Backticks quotation in the string and given the detailed overview regarding concatenation, escaping, and how anyone can access individual character using length property.

>Continue to Lesson 3

Javascript Lessons for beginners



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?