Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:47:47+00:00 2026-05-28T04:47:47+00:00

I thought I had correctly followed the recommendations in this question for checking undefined

  • 0

I thought I had correctly followed the recommendations in this question for checking undefined:

if(typeof window.session.location.address.city != "undefined")       
    console.log(window.session.location.address.city);

But the code above generates this error:

 Uncaught TypeError: Cannot read property 'city' of undefined

What’s the correct way to perform this check?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:47:47+00:00Added an answer on May 28, 2026 at 4:47 am

    Check for the existence of each property:

    if (window.session && session.location && session.location.address)
        console.log(session.location.address.city);
    

    That may log undefined, but will not result in an error. If you only want to log city if it is not undefined, just add in a && typeof session.location.address.city != "undefined". We use typeof in this case because if city contains an empty string or null, it will also evaluate to false (ie, it is “falsey”). Of course, if you only want to log city if it has a value, leave off the typeof and just check to see if it evaluates to true (ie, it is “truthy”) the same way as the others.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem like a programming 101 question and I had thought I knew
This is just an idle thought I had when reading this other question: What
I thought I had seen a bug report about this on the jQuery site,
I thought I had this figured out but it turns out I'm just deleting
Alright, I thought I had this whole setTimeout thing perfect but I seem to
Yesterday i had a question in an interview which i thought i could find
I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
I’m having trouble getting a AJAX/JSON function to work correctly. I had this function
I thought i had this unicode thing down. Then i realize i wasnt saving/parsing
wonder if anyone can help - I thought I had solved this but i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.