NaN error on subtraction of selectedIndex

Why would the below code return NaN:

alert("DOES NOT WORK - RETURNS NaN:"+document.pollform.month[document.pollform.month.selectedIndex].value - 1);

if I remove the subtraction it works fine???

alert("WORKS:"+document.pollform.month[document.pollform.month.selectedIndex].value - 1);
894359Asked:
Who is Participating?
 
RoonaanConnect With a Mentor Commented:
Use:

alert("DOES NOT WORK - RETURNS NaN:"+(document.pollform.month[document.pollform.month.selectedIndex].value - 1));

-r-
0
 
894359Author Commented:
This is what is actually failing:

age.setMonth(document.pollform.month[document.pollform.month.selectedIndex].value-1);
0
 
ZvonkoSystems architectCommented:
Check this:

    alert("WORKS:"+(document.pollform.month.value-1))


One question for explanation: does JavaScript evaluate the statement expression from left to right or right to left?
And if it is from left to right, what number should yield this:  ""DOES NOT WORK - RETURNS NaN: 9"-1;

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
vikrant4u2Commented:
try using the way roonaan has suggested.

i think alert tries to evaluate the msg passed to it.

0
 
ZvonkoSystems architectCommented:
Your var age is NOT an Object of type: Date
0
 
894359Author Commented:
It is I just didn't post the whole code!

var age = new Date();
0
 
ZvonkoConnect With a Mentor Systems architectCommented:
If one of the names, the form or the select, is wrong spelled, like it Month, or PollForm, then you get "undefined": document.pollform.month
Check twice the month select name. Is it perhaps "months"?


0
 
894359Author Commented:
I found the issue. it was on the getting of the dates

age.getMonth vs age.getMonth()



0
 
894359Author Commented:
I should have stated my question better! :(
0
 
ZvonkoSystems architectCommented:
You can ask for delete for this question here: http:/Community_Support/askQuestion.jsp
0
All Courses

From novice to tech pro — start learning today.