user

Shilpa

2 Mar 2022

javascript: Uncaught TypeError: undefined is not a function

Javascript

What did I miss?

var userJoinedDate = new Date().setDate(3);
document.getElementById("result").innerHTML = userJoinedDate.getDate();

it returns error `Uncaught TypeError: undefined is not a function`.

Comments

Rakshit

5 Mar 2022

Best Answer

best answer

Reason for issue: When you use setDate, it modified the object and so it will return undefined, as it wasn't defined internally.

When you change the day, you could copy userJoinedDate object and then change it to the anotherDate name.

var userJoinedDate = new Date();
var anotherDate = new Date(userJoinedDate.getTime());
anotherDate.setDate(3);
document.getElementById("result").innerHTML = anotherDate.getDate();

Try in this way, it will never give undefined error.

Shilpa

6 Mar 2022

github

Thanks, It worked! Never thought of such a solution!

© 2024 Copyrights reserved for web-brackets.com