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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.