find the current year in javascript

In JavaScript, you can find the current year using the Date object and the getFullYear() method.

Here's an example code block:

index.tsx
const now = new Date();
const currentYear = now.getFullYear();
console.log(currentYear); // Output: 2021 (depending on the current year)
137 chars
4 lines

We first create a new Date object using the new keyword. This will initialize the object with the current date and time by default.

We then call the getFullYear() method on the Date object to retrieve the current year. This method returns a number representing the year (such as 2021).

Finally, we log the value of currentYear to the console using console.log(). This will print the output to the browser console (or the terminal console if you're using JavaScript outside of a web browser).

related categories

gistlibby LogSnag