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

jquery undefined function
Publish date: 2022-02-28 | Comments: 1

Tag: Javascript

How to move a div with its content to TinyMEC editor?
Publish date: 2022-02-11 | Comments: 0

Tag: Javascript

JAVASCRIPT: How do I add another key to object?
Publish date: 2022-03-06 | Comments: 3

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

What is the use of package-lock JSON file?
Publish date: 2022-02-11 | Comments: 3

Tag: Javascript

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

Tag: Javascript

JavaScript variable for Changing CSS Height
Publish date: 2022-02-12 | Comments: 1

Tag: Javascript