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)

2
  • @Trailcoder can u try document.write( 1 && null ); Commented Mar 22, 2013 at 7:01
  • Add this to make it work: var NULL = null; Commented Mar 22, 2013 at 7:04

7 Answers 7

4

NULL does not exist, try this

try {
    document.write( 1 &&  NULL  );
} catch ( e) {
    document.write( 1 &&  null  );
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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

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

Comments

0

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

Comments

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

Comments

0

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.