Categories: Javascript Tutorial

Javascript Tutorial – String in Javascript

String in Javascript

What is String

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.

Declaration of string

let name = "QA Peddia Hindi"; -> Double codes
let name = 'QA Peddia Hindi'; -> Single codes
let name = `QA Peddia Hindi`; -> Template literals
Console.log(name);

Attributes of String

How to get the length of a string.

Console.log(name.length);

How to access Characters of a string

Console.log(name[0]);
Console.log(name[1]);
Console.log(name[2]);
Console.log(name[3]);
Console.log(name[5]);

Use of Template literals

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.

Escape Sequence Characters.

\’ – 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

String Methods

Change the characters of a string to Upper Case

This method changes the characters of the string to Upper Case.
This method doesn’t modify the original string.

name.toUpperCase()

Change the characters of a string to Lower Case

This method changes the characters of the string to Lower Case.
This method doesn’t modify the original string.

name.toLowerCase()

Slice the String into 2 Parts – Cut a part of string

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.

Replace a part of string

This method doesn’t modify the original string.

name.replace("Peddia", "Talks")

Concat 2 Strings – Joining 2 Strings

let name1 = "Suresh";
let name2 = "Mahesh";
name1.concat(name2);
name1.concat(" and ", name2, " are students");

Trim method

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.

Strings are immutable

  • A very important thing to note is that Strings are immutable.
  • Once we declare a string, We can not change it’s value, it is going to be same always.
  • We can never delete a string. It is always going to be there in the database.
  • None of the string method can change the value of a string, it generates a new string, but do not change the value of the string.

Summary

We have covered below topics in this session

  1. What is String
  2. How to declare a String
  3. Attributes of a String
  4. String methods or function
  5. Strings are immutable

Admin

Working Professional with more than 12 Years of Experience in Software Testing with Automation and Manual Testing Knowledge.

Recent Posts

Objects, Classes and Constructor in Javascript

Objects, Classes and Constructor in Javascript Classes and Objects A class in any programming language…

55 years ago

OOPs in Javascript

We will start from object oriented programming basics like What are different concepts in Object…

55 years ago

Why we can not over ride Constructor

Why we can not over ride a Constructor This is a very important question, that,…

55 years ago

Javascript Tutorial – Arrow Function in Javascript

What is Arrow Function In the last session, We had discussed about Anonymous Function. Anonymous…

55 years ago

Javascript Tutorial – Anonymous Function in Javascript

What is Anonymous Function Anonymous Function is a type of function which has no name…

55 years ago

Javascript Tutorial – Map, Filter and Reduce

What is Map, Reduce and Filter Map Filter and Reduce are higher order methods for…

55 years ago