473,416 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

What does "if(!variable)" check?

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?
Jun 27 '08 #1
3 1994
Yansky <th*********@gmail.comwrites:
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.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jun 27 '08 #2
"Yansky" <th*********@gmail.com>
>
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/pu...T/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
Jun 27 '08 #3
On Apr 13, 7:01 am, Joost Diepenmaat <jo...@zeekat.nlwrote:
Yansky <thegoodd...@gmail.comwrites:
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.

--
Joost Diepenmaat | blog:http://joost.zeekat.nl/| work:http://zeekat.nl/
Thanks, I get it now.
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Derek Fountain | last post by:
The documentation says session_destroy() "destroys all of the data associated with the current session". Um, like what? The docs further say that you should remove all information in the _SESSION...
2
by: pembed2003 | last post by:
Hi all, I recently saw a piece of code that looks like: class one{public: one(){} }; class two : public virtual one{public: two(){} }; class three : virtual public one{public: three(){} }; ...
2
by: Alexander Stippler | last post by:
Hi, what about "not a number"? What does the standard enforce, what not? I read in the C standard, that NAN is defined if and only if the implementation supports quiet NaNs for the float type....
13
by: ranjeet.gupta | last post by:
Dear All What does exactly below code means struct File { void* data; }; typedef struct File File; typedef File* fl;
4
by: **Developer** | last post by:
I have a usercontrol that contains the following. To my surprise the form containing this control get KeyUp events. Help says that for KeyPress setting e.Handled = True suppresses KeyPress...
2
by: needin4mation | last post by:
I was looking at this code, and have seen it in other code (like it): /// <summary> /// Bitmap font class for XNA /// </summary> public class BitmapFont { private SpriteBatch m_sb; private...
4
by: Karen Hill | last post by:
How does one check if an object's reference is null? I'd like to check that rather than catch a nullreference exception. thanks.
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
38
by: larry | last post by:
I am looking at some code which process an uploaded file and it has loops like: for(;;){ // for([two semicolons){ what is the function of the two semicolons? loop until break? I've...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.