ngplayground
ngplayground

Reputation: 21617

Javascript if variable is not null

if (typeof (location.x != null) {

Using the above I run into the follow error: Uncaught TypeError: Cannot read property 'x' of null

I have tried console.log(location.x) which results in an example with null

Upvotes: 2

Views: 801

Answers (3)

Sébastien P.
Sébastien P.

Reputation: 544

The x attribute can't be reached because your location variable is null

Upvotes: 1

Fabio
Fabio

Reputation: 1960

if (location && location.x) {

}    

Upvotes: 8

tymeJV
tymeJV

Reputation: 104775

You can just do:

if (location && location.x) {

}

Upvotes: 2

Related Questions