Age calculator program in javascript
Here you will learn to create an age calculator program in javascript language.
This program takes a birth year as input, calculates the age based on the current year, and outputs the result to the console. You can change the birth year in the calcAge function call to calculate the age for a different year.
Program code of age calculator program in javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // calculate age function function calcAge(birthYear) { var currentDate = new Date(); var currentYear = currentDate.getFullYear(); var age = currentYear - birthYear; return age; } // enter birth year var birthYear = prompt("Please enter your birth year: "); // calling Calculate age function var age = calcAge(birthYear); // Output age console.log("You are " + age + " years old."); |
Output
Please enter your birth year: 1990
You are 34 years old.
Check out our other JavaScript examples