Why not || seems to work like the coalesce / default statement in JavaScript?

I've seen a couple of web pages that say "a = b || 'blah" should assign to' blah 'if b is undefined or null. But if I type this in Firebug or use it in code, it complains that b is not defined in the list on FF3 / win. Any hints?

Edit: I'm looking for a case where b might not exist at all. For example a DOM node with no ID.

+1


source to share


4 answers


I think you are looking for this:



var a = typeof b == 'undefined' ? 'blah' : b;

      

+3


source


If b existed and was false, null, etc., then it works as you'd expect. All you have to do is on the line above this, put var b = null

;

It makes sense if you think about it. It basically does something like this ...

a = function(){ if(b){return b;} else{ return 'blah' } }();

      

Note that this is a test that the value of b is true ... if b does not exist, you get an exception.

About Undefined Variables

"Undefined" in javascript does not mean that "the variable does not exist". This means that "the value of a variable is a special value undefined

". Example:

alert(nosuchvariable);
=> throws exception

var somevariable; // note it never assigned
alert(somevariable);
=> This alerts with 'undefined'

      

As for checking if variables exist.

So, if we try to read b

and there is no such variable as b, we will get an exception. If we are trying to find out if b is defined then it does not help.

You can see if global variables exist by checking the window

top level object . All global variables are actually just fields in the object window

. Example:

foo = 'Hello';
alert( window.foo );
=> alerts 'Hello'

      



Since you know that the window object already exists, you can check its fields.
Checking for fields that don't exist in javascript will give you undefined

and won't crash, so you can do a concatenation or put undefined

in a variable or whatever

For local variables (things declared with var

), you cannot check for their existence. they don't "live" anywhere, global variables "live" in the window object, and any normal attempt to reference them will throw an exception: for example:

alert(a);
=> exception because a is meaningless
alert(d45pwiu4309m9rv43);
=> exception because that is equally meaningless

      

However, there is one exception (which I know, thanks to J c in the comments), operator typeof

. If you try to get the type of something that doesn't exist, it won't work, it will return a string "undefined"

.
This gives you the ability to check for non-existent local variables. eg:

if( typeof(djfsd) === "undefined" )
  alert('no such variable');

      

As for DOM elements that don't exist

There have been several comments mentioning DOM elements without an ID, etc ...

The fact that the DOM element doesn't really matter. Think of the DOM as a database or file, and an element as a string in that database or a word in that file. To do something about it, you need to search the database, find the correct row, and pull the data. The data goes into a javascript object. Then you access it by manipulating that object and possibly putting the object in a variable if you like. Example:

document.getElementById('foo');

      

this goes to dom and looks for element with id 'foo'. If it finds one, it puts some information about that element into a javascript object and then passes that object back to you. If the element cannot find the element, it will return it to you null

, but all the normal rules will still apply (you can inject null

into a variable or whatever).

This does not affect coalescence at all.

+8


source


||

is a short-circuited logical OR operator in Javascript, just like in C, C ++, Java, C #, Perl, PHP, etc.

According to Wikipedia , if you put parentheses around b, it works as you expect.

var a = (b) || 'blah';

      

0


source


But if I type this in Firebug or use it in code, it complains that b is not defined, listed on FF3 / win

What does "not defined" mean? Do you mean Javascript doesn't know the variable? Then you can use window.b

as "window" is a top-level object or declares b first with var b;

; but only if it's a variable.

If it is a DOM element, you may need to start it first, for example with document.getElementById

:

a = document.getElementById('b') || 'blah'

      

works for me.

0


source







All Articles