JavaScript | Text Formatting

JavaScript has many inbuilt features to format the text. Texts are the strings in javascript. There are many formatting styles like making text uppercase, lowercase, bold, italic. Each formatting style is given below with the example.

Making text UpperCase: It is used to convert the string to uppercase.

Javascript




let text = "Beginner for Beginner"
console.log("text before formatting is " + text);
text.toUpperCase()
console.log("text after formatting is " + text);


Output:

Making text LowerCase: It is used to convert the string to lowercase.

Javascript




let text = "Beginner FOR Beginner"
console.log("text before formatting is " + text);
text.toLowerCase()
console.log("text after formatting is " + text);


Output:

Use of Substr: It is used to take the substring from the string. The first parameter is the index from which the string has to start and the second parameter is the length of substring.

Note: Please note that the second parameter is not the index.

Javascript




let text = "Beginner FOR Beginner"
text = text.substr(10, 5)
console.log("substring is " + text);


Output:

Use of repeat: Use of repeat is done when we need to repeat a particular string or substring to be printed in a particular number of times.

Javascript




let text = 'Beginner for Beginner ';
console.log(`string is repeated 2 times: ${text.repeat(2)}`);


Output:

Use of trim: It is used to remove the extra spaces around the string.

Note: It does not remove the spaces from between the text and words.

Javascript




let txt = '                  Beginner for Beginner            ';
console.log(`string with no trailing spaces, ${txt.trim()}`);


Output:



Contact Us