How to access Months Array in Javascript and jQuery
You can create an array containing the names of the months and access specific months using their list.
Using Javascript
Here’s an example of how you can do this in Javascript:
javascript code snippet:
const months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
// or
const months2 = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"];
// Accessing months using indices (0-based)
const firstMonth = months[0]; // January
const sixthMonth = months[5]; // June
Using jQuery
The code snippet below shows how to do it in jQuery:
jquery code snippet:
<!-- Include jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
const months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
// Accessing months using indices (0-based)
const firstMonth = months[0]; // January
const sixthMonth = months[5]; // June
console.log(firstMonth);
console.log(sixthMonth);
});
</script>
popular readings: