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,816
  • 9
  • 44
  • 66

7 Answers7

4

NULL does not exist, try this

try {
    document.write( 1 &&  NULL  );
} catch ( e) {
    document.write( 1 &&  null  );
}
rab
  • 4,134
  • 1
  • 29
  • 42
1

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

Hubro
  • 56,214
  • 69
  • 228
  • 381
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
  • 63,804
  • 18
  • 124
  • 206
0

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

ulentini
  • 2,413
  • 1
  • 14
  • 26
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
  • 47,944
  • 19
  • 150
  • 166
0

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

Nasir Mahmood
  • 1,445
  • 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
  • 170,779
  • 38
  • 263
  • 309