Strings are one of the data types of JavaScript, where they are used to store sequence of characters.
' 'or" "- used to represent values of a string.String.length- used to find how many characters are present in the string.string[2]- used to get the specific character
- There are two ways to concatenate a two or more strings.
-
Using
+operator.let firstName = "Ratan"; let lastName = "Tata"; let fullName = firstName + " " + lastName;
-
Using Template Literals.
let firstName = "Ratan"; let lastName = "Tata"; let fullName = `${firstName} ${lastName}`;
- There are many things we can do with Template literals.
- Define strings.
let avenger = `ironman`
- Quote inside strings.
let stringWithQuote = `He's often called "Ironman"`
- Define multiline strings.
let multilineString = `He's often called "Ironman"`;
- Define interpolate variables and expressions into strings.
let firstName = "Ratan"; let lastName = "Tata"; let fullName = `${firstName} ${lastName}!`; let radius = 10; const PI = 3.14; let areaOfCircle = `Area of circle: ${PI * (radius**2)}`;
- Define strings.
- There are many things we can do with Template literals.
-
toLowerCase()- used to make a string to lower case characters.toUpperCase()- used to make a string to upper case characters.indexOf('')- used to get the index of particular character.- if the character is present, it will return a number.
- if the character is not present, it will return
-1number as result.
lastIndexOd('')- same asindexOf()but finds the last occurrence.slice()- used to get the sub string from a string by takingstartandendindex values as input.
let avenger = "Ironman";
let lowerCaseOfAvenger = avenger.toLowerCase();
let upperCaseOfAvenger = avenger.toUpperCase();
let indexOfN = avenger.indexOf("n");
let lastIndexOfN = avenger.lastIndexOf("n");
let slicedAvenger = avenger.slice(2, 5);