/    Sign up×
Community /Pin to ProfileBookmark

Unnecessary evaluation against TRUE ?

This is a fragment from the book “Beginning Javacript 5th edition”.
Chapter 3.


——-

[I]Look at an example in which you want to get the user’s age via a prompt box. You want to show the prompt box but also make sure that what the user has entered is a number:[/I]

[CODE]var userAge;
do {
userAge = prompt(“Please enter your age”,””)
} while (isNaN(userAge) == true);[/CODE]


——-

I was wondering, what is the purpose of comparing [B]isNaN(userAge) [/B] against true, since [B]isNaN(userAge) [/B] would evaluate to [B]true[/B] all by itself… right?

Thanks in advance!
Bruno.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@rootJan 14.2017 — isNaN is a test for if an entry is not a number and if true, the loop goes around again for another input.
Copy linkTweet thisAlerts:
@Clock_OutJan 15.2017 — Sometimes you want to be explicit. If you have to read the code again the programmer's intention remains crystal clear.

For example I was checking if a property existed on an object.

<i>
</i>// Original
if (myObj[propName]) { //TO DO }

// Revised
if (myObj.hasOwnPropertyName(propName)) { //TO DO }


Things would have worked fine with the original version but I decided to be explicit about what I wanted so that future me wouldn't puzzle over it.
Copy linkTweet thisAlerts:
@lon124Jan 16.2017 — brsastre, you are completely correctly, it's redundant. As . said, it might have been done for code clarity. Strictly speaking they could have used '===' instead of '=='.

Clock Out, I think in your example the code behaves slightly differently. If you assign the value false to myObj[propName], the first if-statement will fail but the second will pass.
Copy linkTweet thisAlerts:
@Clock_OutJan 17.2017 — Exactly correct Ion124 but that wasn't a concern where this code appeared.
Copy linkTweet thisAlerts:
@shakazorbaJan 17.2017 — var userAge;

do {

userAge = prompt("Please enter your age","")

} while (isNaN(userAge) == true);

if the user enters something other than

a number the loop continues

if the user enters a number

the loop is over

this to to ensure the user

enters a number
Copy linkTweet thisAlerts:
@rootJan 18.2017 — var userAge;

do {

userAge = prompt("Please enter your age","")

} while (isNaN(userAge) == true);

if the user enters something other than

a number the loop continues

if the user enters a number

the loop is over

this to to ensure the user

enters a number[/QUOTE]


[B]isNaN[/B] returns [B]TRUE[/B] or [B]FALSE[/B] so you don't need to check for its result, that is completley pointless because the loop will run if the result of no numerical input making a [B]TRUE[/B] return, the loop terminates if [B]FLASE[/B] is returned because a number is passed via the input.
×

Success!

Help @brsastre spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...