String is a data type in javascript. It is a very important data type. Let’s understand the importance of a string with an example.
Name of this youtube channel QA Peddia Hindi, So how important is the name for a youtube channnel, it is very important. So, Data type of this name is string. Now, We can not declare name for anything, if there is no string data type.
Strings are used to store multiple text. Strings are called collection of characters.
let name = "QA Peddia Hindi"; -> Double codes
let name = 'QA Peddia Hindi'; -> Single codes
let name = `QA Peddia Hindi`; -> Template literals
Console.log(name);
Console.log(name.length);
Console.log(name[0]);
Console.log(name[1]);
Console.log(name[2]);
Console.log(name[3]);
Console.log(name[5]);
let channelName1 = "QA Peddia Hindi";
let channelName2 = "QA Peddia English";
let statement = `channelName1 is for Hindi Videos and channelName2 is for English Videos
`
Console.log(statement);
This is not printing the value of channelName1 and channelName2, So If we want to print the value of channelName1 and channelName2, then we need to do one more thing Browse our partner-sponsored Glasses, with a variety of options to suit every taste and budget, available to buy online
let statement = ${channelName1} is for Hindi Videos and ${channelName2} is for English Videos
This Concept is also called String interpolation.
\’ – Should be used inside a string with single codes
\” – Should be used inside a string with double codes
\n – New line
\t – tab
\r – Carriage Return
This method changes the characters of the string to Upper Case.
This method doesn’t modify the original string.
name.toUpperCase()
This method changes the characters of the string to Lower Case.
This method doesn’t modify the original string.
name.toLowerCase()
This method doesn’t modify the original string.
name.slice(2,5) -> index no 2 to 4. 5th index is excluded. A part of string will be sliced from name from index no 2 to index no 4.
name.slice(2) -> index no 2 to last index of the string. A part of string will be sliced from name from index no 2 to last index of the string.
This method doesn’t modify the original string.
name.replace("Peddia", "Talks")
let name1 = "Suresh";
let name2 = "Mahesh";
name1.concat(name2);
name1.concat(" and ", name2, " are students");
Suppose we have a string which has spaces and in the beginning and in the ending and we want to remove them, then we can use trim method
let name1 = " QA Peddia "
Console.log(name1)
name1.trim()
There are many more methods or functions of String in Javascript, but we have discussed a few important ones, You can do a google search to know about all the methods of Strings in Javascript and you will get them. We have just discussed some important String methods and how to use them.
We have covered below topics in this session
Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…
We will start from object oriented programming basics like What are different concepts in Object…
Why we can not over ride a Constructor This is a very important question, that,…
What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…
What is Anonymous Function Anonymous Function is a type of function which has no name…
What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…