Skip to content
Related Articles

Related Articles

JavaScript | Text Formatting
  • Last Updated : 05 Jun, 2020

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 = "geeks for geeks"
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 = "GEEKS FOR GEEKS"
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 = "GEEKS FOR GEEKS"
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 = 'geeks for geeks ';
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 = '                  geeks for geeks            ';
console.log(`string with no trailing spaces, ${txt.trim()}`);

Output:

full-stack-img

My Personal Notes arrow_drop_up
Recommended Articles
Page :