#6 Javascript Strings | Javascript Hindi Course For Beginners ( 2023 )

Basics of string

String → A string is a sequence of characters used to represent text. Strings are defined using quotes (either single or double).
// Double Qoutes -> " " let name = "Anshu Raj"; // Single Qoutes -> ' '; let channel = 'Do Some Coding'; // Template Literals -> ' '; let newone = `This is called template literals`; // these are three ways to assgin strings

Escape Characters

let message = 'Don't do this'; // above line will throw string error // as we are using single qoutes inside string // how to fix this issue // 1. First Way let message = "Don't do this"; // if are using single qoutes inside string then use double qoutes outside let message = 'Don"t do this'; // if are using double qoutes inside string then use single qoutes outside // 2 . Second Way let message = 'Don\'t do this'; // use \ after single qoutes that is inside it will fix it let message = "Don\"t do this"; // use \ after single qoutes that is inside it will fix it
More Escape Sequence
// for creating new line inside string -> " \n " const message = "This is best course on entire youtube. \n I am providing Pratice Questions too " // for creating line break inside string -> " \r " const message = "This is best course on entire youtube. \r I am providing Pratice Questions too " // for creating space inside string => " \t " const message = " Subscribe \t this channel ";
Template Literals
// Template Literals helps you to run javascript code inside string const name = "Anshu Raj" const message = `Anshu Raj is Developer.` const message2 = `${name} is Developer.` // new method const message3 = name + 'is Developer.' // old method

String Methods

 
const channel = "Do Some Coding"; // Change String to Upper Case channel.toUpperCase(); // -> "DO SOME CODING" // Change String to Lower Case channel.toLowerCase(); // -> "do some coding" // Trim Text from String const greeting = " Hello world! "; console.log(greeting); // Expected output: " Hello world! "; console.log(greeting.trim()); // Expected output: "Hello world!"; // Get length of string channel.length; // -> 14 // Get character at index channel.charAt(0); // -> "D" // Get character code at index channel.charCodeAt(0); // -> 68 // Match o in string channel.match(/o/g); // -> ["o", "o"] // Replace Coding with Learning channel.replace("Coding", "Learning"); // -> "Do Some Learning" // Split string into array channel.split(" "); // -> ["Do", "Some", "Coding"] // Continate string "Do Some Coding" with " is fun" channel.concat(" is fun"); // -> "Do Some Coding is fun" // Get Sub String from index 3 to 7 channel.substring(3, 7); // -> "Some" // Check channel start with "Do" channel.startsWith("Do"); // -> true // Check channel end with "ing" channel.endsWith("ing"); // -> true // Check channel includes "Some" channel.includes("Some"); // -> true // Get index of "Some" channel.indexOf("Some"); // -> 3 // Get last index of "o" channel.lastIndexOf("o"); // -> 11 // Search string const paragraph = "The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?"; // Any character that is not a word character or whitespace const regex = /\bDog\b/gi; paragraph.search(regex); // -> 40
 
Questions:
  1. Try to replace “dog” with “cat” from Sample String → “ he quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?”
  1. Write a function to convert string to lowercase;
  1. Check “Fox” is includes . Sample String → “ he quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?”
  1. Check String end with “?” . Sample String → “ he quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?”
  1. Extract Amount from string → “ I bought car at ₹ 550000 “