Home » JavaScript

Capitalize words in a string in JavaScript

Capitalize string in JavaScript: Here, we are going to learn how to capitalize words in a given string in JavaScript?
Submitted by Shivang Yadav, on July 30, 2019

String in JavaScript is a sequence of characters. And capitalizing characters of words in a JavaScript string will change each character of the string with the capital letter of it.

To convert words to uppercase we have the following method defined by slicing each character and then converting it to uppercase.

Components are defined as:

Slice():
The slice method will return the specified element of the object and returns it to another object.

For slice(3) for the array [3,5,6,8,9] will give 8.

toUpperCase():
The method will return the uppercase changed the value of the character. For example: for 'f' input, the output is 'F'.

charAt():
returns the charter at the given index.

Program to capitalize each character of the sting using JavaScript

< script >
	function capitalizeEachWord(str) {
		var cstr = '';
		str.split('').forEach(function(char) {
			cstr = cstr + char.toUpperCase();
		});
		return cstr;
	}
	console.log("Hello, world!")
	var x = "learning JavaScript at include help"
	var y = capitalizeEachWord(x);
	console.log('x = ' + x);
	console.log('y = ' + y); 
</script>

Output

Hello, world!
learning JavaScript at include help
LEARNING JAVASCRIPT AT INCLUDE HELP





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.