By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,747 Members | 1,782 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,747 IT Pros & Developers. It's quick & easy.

can I avoid the evail to convert from string to a boolean

P: n/a
Hi,
Is there a better way to do this?
The problem that I have is that x can be "true" or "false" (as type
string), or true or false (as boolean type). I'm doing comparisons
like...

if (eval(x) == true){
// do something...
}

or..

if (eval(x) == false){
// do something...
}

I'd like to get rid of the eval, I tried to use the !!, but if I do !!x
when x is "false", it returns true since it's not an empty string. Do
you know a better way to do this? Without forcing x to be always a
string or always a boolean?

Thanks,
Carlos

Oct 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Carlos Aguayo wrote:
Hi,
Is there a better way to do this?
The problem that I have is that x can be "true" or "false" (as type
string), or true or false (as boolean type). I'm doing comparisons
like...

if (eval(x) == true){
// do something...
}

or..

if (eval(x) == false){
// do something...
}

I'd like to get rid of the eval, I tried to use the !!, but if I do !!x
when x is "false", it returns true since it's not an empty string. Do
you know a better way to do this? Without forcing x to be always a
string or always a boolean?
Though it's redundant to do so on a string, you can do the following:

if(x.toString() == "true")
{
//etc.
}

[...]

if(x.toString() == "false")
{
//etc.
}

This will work for both booleans and strings.

Oct 6 '06 #2

P: n/a
JRS: In article <11**********************@h48g2000cwc.googlegroups .com>,
dated Fri, 6 Oct 2006 16:49:42 remote, seen in
news:comp.lang.javascript, Carlos Aguayo <ca***********@gmail.com>
posted :
>Is there a better way to do this?
Yes.
>The problem that I have is that x can be "true" or "false" (as type
string), or true or false (as boolean type).
Within a program, away from I/O, a logical value with exactly two
significantly-different states should be a Boolean, and not a String.

You should convert the value to genuine Boolean as soon as it is
generated, if it is not already Boolean. The type will be known at
creation.

You should not ask your users to put "true" or "false" in a control that
inputs strings; you should use a checkbox instead.

If you are obliged to accept such strings, then convert to lower case,
compare with both "true" and "false", and decide what to do if neither
match.

It's a good idea to read the newsgroup and its FAQ. See below.
--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.