473,666 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What does "if(!variab le)" 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 2009
Yansky <th*********@gm ail.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*********@gm ail.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.n lwrote:
Yansky <thegoodd...@gm ail.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
6506
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 global with $_SESSION = array() and you should use setcookie() to set the session cookie to a blank value. Having done those, what does that leave session_destroy() to do? The page at http://au2.php.net/manual/en/function.session-destroy.php...
2
2152
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(){} }; My questions: 1. What does it mean to have a virtual inheritenec? When do you want
2
2774
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. But what about the behaviour of NaNs, e.g. comparison with 'normal' values, operator application (+, -, *, /). Where is it defined, if at all? regards, alex
13
2185
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
6966
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 events but say very little about KeyUp. What does setting to Handled do?
2
2318
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 Dictionary<int, BitmapInfom_dictBitmapID2BitmapInfo;
4
10969
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
6187
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
6087
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 tried to google it but the text filter seems to remove the semicolons.
0
8454
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8362
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8878
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8644
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6200
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5671
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.