79666062

Date: 2025-06-14 18:28:23
Score: 0.5
Natty:
Report link

When we say new Date() we are essentially creating a new instance/object of the class Date using Date() constructor method. When we call the Date() method without the use of new keyword it actually returns a String not an instance/object of the class Date. And a string will not contain the method getFullYear();. Hence we get an error

Now consider the below code snippet:

let dateTimeNowObj = new Date(); // returns a object of class Date
console.log(dateTimeNowObj) // Sat Jun 14 2025 23:48:27 GMT+0530 (India Standard Time)
console.log(dateTimeNowObj.getFullYear()); // 2025

let dateTimeNowStr = Date(); // returns a string 
console.log(dateTimeNowStr) // Sat Jun 14 2025 23:47:32 GMT+0530 (India Standard Time)
console.log(dateTimeNowStr.getFullYear()); // TypeError: dateTimeNowStr.getFullYear is not a function
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When we
  • Low reputation (1):
Posted by: Shrivatsa Shetty