3

This is the code I am testing --

Works fine

document.write( 1 && undefined ); // prints undefined
document.write( 1 && 3 ); // prints 3 
document.write( 1 && true ); // prints  true

Throws error

document.write( 1 && NULL ); // throws Error 

why using NULL throws arror although its working even for undefined

although i tested typeof NULL and its giving undefined but still its not working.let me know about this. (New to OOP programming)

Trialcoder
  • 5,392
  • 9
  • 40
  • 65

7 Answers7

4

NULL does not exist, try this

try {
    document.write( 1 &&  NULL  );
} catch ( e) {
    document.write( 1 &&  null  );
}
rab
  • 4,066
  • 1
  • 28
  • 40
1

NULL is undefined because it doesn't exist. You're thinking of null.

Hubro
  • 52,462
  • 63
  • 210
  • 362
1

document.write(1 && null); outputs null.

NULL does not exist in JavaScript because it's case-sensitive. It must be null.

Matías Fidemraizer
  • 61,574
  • 17
  • 117
  • 195
0

It's null (lowercase), not NULL (uppercase)

ulentini
  • 2,392
  • 1
  • 13
  • 25
0

Because undefined is different to a symbol that doesn't exist, the browser is throwing an error. From the Chrome console:

> 1 && null
null
> 1 && NULL
ReferenceError: NULL is not defined
> NULL
ReferenceError: NULL is not defined
Daniel Imms
  • 45,529
  • 17
  • 143
  • 160
0

read this may be it will answer your question JavaScript undefined vs. null

Nasir Mahmood
  • 1,395
  • 1
  • 13
  • 18
0

Using typeof something only gives the type of that expression; it's undefined in this case and so using that symbol naturally gives an error. It's the same as:

typeof unknownvar
// "undefined"
unknownvar
// ReferenceError: unknownvar is not defined

The exception is the symbol undefined itself:

typeof undefined
// "undefined"
undefined
// undefined

In your particular case, NULL should be null.

Ja͢ck
  • 166,373
  • 34
  • 252
  • 304