user image

Shilpa
Published in : 2022-03-02

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 Date : 2022-03-05

Best answers

34

Best answers

34

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 Date : 2022-03-06

Best answers

10

Best answers

10

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

Leave a comment

Join us

Join our community and get the chance to solve your code issues & share your opinion with us

Sign up Now

Related posts

Coping plain text into form bug.
Publish date: 2022-02-11 | Comments: 1

Tag: Javascript

Javascript Add (+) issue
Publish date: 2022-02-26 | Comments: 1

Tag: Javascript

Help: How do I loop simple JSON objects to iterate all keys and values itself?
Publish date: 2022-03-06 | Comments: 1

Tag: Javascript

How do I check if an array includes a value in JavaScript?
Publish date: 2022-02-13 | Comments: 2

Tag: Javascript

Method refactoring to avoid to many npe checks
Publish date: 2022-03-05 | Comments: 0

Tag: Javascript

Can we add a javascript or jQuery to the URL?
Publish date: 2022-03-05 | Comments: 2

Tag: Javascript