What does "if(!variable)" check?

Y

Yansky

If I have the following code:

var abc;

if(!abc){

alert('test');

}

Does "if(!abc)" check to see if the variable is null or does it check
to see if the variable exists at all?
 
J

Joost Diepenmaat

Yansky said:
If I have the following code:

var abc;

if(!abc){

alert('test');

}

Does "if(!abc)" check to see if the variable is null or does it check
to see if the variable exists at all?

In that code, abc always exists.

Anyway ! EXPRESSION:

Throws a reference error if the EXPRESSION resolves to a property of
which the base object can't be determined; loosly: an undeclared
variable. (As far as I can tell. This part of the specs has some pretty
deep implied consequences).

If the expression does resolve to a value (which includes declared but
not explicitly initialized global variables and never-before used
properties of explicit objects, which will be undefined) it will first
convert the EXPRESSION to a boolean and then return the inverse.

The standard ToBoolean conversion is:

undefined -> false
null -> false
boolean -> boolean
numbers 0, -0, NaN -> false
other numbers -> true
empty string -> false
other string -> true
object -> true

See ecma 262 3rd edition, section 9.2.
 
T

Tom de Neef

Yansky said:
var abc;

if(!abc){

alert('test');

}

Does "if(!abc)" check to see if the variable is null or does it check
to see if the variable exists at all?

ECMA262
(http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf)
11.4.9
the expression abc is evaluated and transformed via toBoolean(abc)
then the NOT operator: if the result is true then return false else return
true

toBoolean(arg) returns false when arg is one of the following:
undefined
Null
boolean - false
number - +0, -0, NaN
String - when empty

Tom
 
J

Jorge

Does "if(!abc)" check to see if the variable is null or does it check
to see if the variable exists at all?

In your example, abc does exist even though its value is set = to
"undefined" that is falsey -->> !undefined is true.

But had it not been declared previously, an exception would have been
thrown:

try { alert(!abc) } catch (e) { alert("Catch") } -->> "Catch"

But :
var abc;
try { alert(!abc) } catch (e) { alert("Catch") } -->> "true"

So I guess that the answer to your question is (AFAIK) :

In order to know if a variable has been declared, a test like that
ought to be inside a try {} block.
 
T

Thomas 'PointedEars' Lahn

Jorge said:
[...]
So I guess that the answer to your question is (AFAIK) :

In order to know if a variable has been declared, a test like that
ought to be inside a try {} block.

In case one wants their code to break in not-so-old implementations on might
just do that.

There is hardly ever a need to determine the existence of a variable -- that
would be indicative of bad programming in the first place --, and where
another property is involved there are better methods than plain type
conversion: the hasOwnProperty() method and a typeof operation as a fallback.

http://PointedEars.de/es-matrix


PointedEars
 
Y

Yansky

In that code, abc always exists.

Anyway ! EXPRESSION:

Throws a reference error if the EXPRESSION resolves to a property of
which the base object can't be determined; loosly: an undeclared
variable. (As far as I can tell. This part of the specs has some pretty
deep implied consequences).

If the expression does resolve to a value (which includes declared but
not explicitly initialized global variables and never-before used
properties of explicit objects, which will be undefined) it will first
convert the EXPRESSION to a boolean and then return the inverse.

The standard ToBoolean conversion is:

undefined -> false
null -> false
boolean -> boolean
numbers 0, -0, NaN -> false
other numbers -> true
empty string -> false
other string -> true
object -> true

See ecma 262 3rd edition, section 9.2.

Thanks, I get it now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top