Tutorial
Revision 4

Revision on Previous Work

🎉

Read This on Github

This article is available on Github. You can read it there and contribute to it.
Github Link
Any Issue ?

⭐ Revision on Previous Work

Let's revise what we have learned so far.

⁉️ Question 1:

What is the output of the following code? and why ?

let str = "Xam\""
console. log( str. length)

The output of this code will be 4. In this case, the string is "Xam"" which contains 4 characters (X, a, m, ") and the backslash is used as an escape character for the quotation mark, so it does not count as a character in the string. So, when we run the console.log(str.length) it will print the number of characters in the string which is 4.

⚡ Playground

⁉️ Question 2:

Explore the includes, startsWith, endsWith methods of the String object.

⚡ Playground

⁉️ Question 3:

Write a program to convert a given string into lowercase.

⚡ Playground

⁉️ Question 4:

Extract the amount out of the following string and print it in the console.

"Please pay 100 for the product"

ℹ️

Code 1 Explain ❓

  • It uses the indexOf() method to find the position of the word "pay" and "for" in the string.
  • str.indexOf("pay")+4 gives the position of the number after the word pay and str.indexOf("for") gives the position of the word for.
  • The slice method will extract the substring between these two positions and will output 100 in the console.
ℹ️

Code 2 Explain ❓

  • It uses the string length of "hey give me" to slice the string and extract the amount.
  • "hey give me".length gives the position after the string "hey give me" and the slice method will extract the substring after this position which is 100 and will output 100 in the console.
ℹ️

Code 3 Explain ❓

  • The third code snippet also extracts the amount from the given string "hey give me 100" using the slice() method of the String object.
  • It uses the index number 11 as the starting point to slice the string and extract the amount.
  • str3.slice(11) will extract the substring after the 11th index which is 100 and will output 100 in the console.
ℹ️

But the type of the result is a string, not a number. So, if you want to use it as a number, you need to convert it to a number using the parseInt() method.

⁉️ Question 50:

Try to change 4th character of the given string let student = "xam"

📕

In JavaScript, strings are immutable by default. So, when you try to change a specific character in a string, it does not change the original string, instead it creates a new string with the modified value. For example, when you use the + operator to concatenate two strings, it creates a new string instead of modifying the original strings.

⚡ Type 1

You can change the 4th character of the given string "xam" by using the substring() method and the concat() method.

Here is an example of how you might use these methods:

let student = "xam";
let newString = student.substring(0,3) + "b";
console.log(newString);

This will create a new string by extracting the substring from the original string from the start index 0 to the index 3 (not including index 3) and then concatenating the character "b" to it. It will output the new string xamb in the console.

⚡ Type 2

You can also use the splice() method to change the 4th character of the given string

Here is an example of how you might use this method:

let student = "xam";
let newString = student.split('');
newString.splice(3,1,'b');
console.log(newString.join(''));

This will convert the string to an array using split('') and then splice the array at the 4th index, replacing it with the new character 'b' and then join the array back to a string and will output the new string xamb in the console.

⚡ Type 3

You can also use destructuring assignment to change the 4th character of the string

Here is an example of how you might use this method:

let student = "xam";
let [a,b,c,d] = student.split('');
console.log(a+b+c+"b");

This will convert the string to an array using split('') and then assign the characters to the variables a, b, c, d. Then, it will output the new string xamb in the console.