Helper Functions

Welcome to our free JavaScript tutorial. This tutorial is based on Webucator's Introduction to JavaScript Training course.

Contact Us or call 1-877-932-8228
Helper Functions

Helper Functions

Some languages have functions that return the month as a string. JavaScript doesn't have such a built-in function. The sample below shows a user-defined "helper" function that handles this and how the getMonth() method of a Date object can be used to get the month.

Code Sample:

BuiltInObjects/Demos/MonthAsString.html
---- C O D E   O M I T T E D ----

<script>
	function monthAsString(num){
		var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
		return months[num-1];
	}

	function enterMonth(){
		var userMonth = prompt("What month were you born?", "");
		alert("You were born in " + monthAsString(userMonth) + ".");
	}
	
	function getCurrentMonth(){
		var today = new Date();
		alert(monthAsString(today.getMonth()+1));
	}
</script>
---- C O D E   O M I T T E D ----

Next

This tutorial is based on Webucator's Introduction to JavaScript Training Course. We also offer many other JavaScript Training courses. Sign up today to get help from a live instructor.