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 8919319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:59:20+00:00 2026-06-15T05:59:20+00:00

I thought I understood how the default operand worked in JavaScript, but clearly not.

  • 0

I thought I understood how the default operand worked in JavaScript, but clearly not. I’m trying to first determine the user’s geographic position, and load a map centered on those coordinates. If the device is unable to determine those coordinates, it would fallback to just loading the map with some default values.

You’ll notice that in my error method, I’m calling gMap.init() with no arguments, which I thought should mean that the variables lat and lon should be set to 57.700992 and 11.893836 respectively. Instead, I’m getting Uncaught TypeError: Cannot read property 'coords' of undefined. Where am I going wrong?

Also, in theApp.init() I’m calling the map if navigator.geolocation exists. Does that mean browsers that don’t support HTML5 geolocation will not even try loading the map?

var gMap = {
    init: function (position) {

            var lat = position.coords.latitude || 57.700992,
                lon = position.coords.longitude || 11.893836,
                geocoder =  new google.maps.Geocoder(),
                mapOptions = {
                    zoom: 12,
                    center: new google.maps.LatLng(lat, lon)
                },
                map = new google.maps.Map(document.getElementById('mapcanvas'), mapOptions);

    },
    error: function () {
        console.log('failed to retrieve geoposition');
        gMap.init();
    }
}


var theApp = {
    init: function () {

        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(gMap.init, gMap.error, {timeout:10000});
        }

    }
}
  • 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-06-15T05:59:22+00:00Added an answer on June 15, 2026 at 5:59 am

    The || you have there should work, unless position.coords or position is undefined. In that case, JavaScript will throw a error because you’re trying to access a property on a undefined object.
    You will have to manually check if the objects exist:

    var lat, lon;
    if(position && position.coords){
        lat = position.coords.latitude || 57.700992;
        lon = position.coords.longitude || 11.893836;
    }
    

    If position is undefined, the if will abort, without trying to check for position.coords.
    (The && doesn’t evaluate the right parameter, if the left one is false)

    And yes, if navigator.geolocation is undefined, the map will not be loaded:

    navigator.geolocation.getCurrentPosition(gMap.init, gMap.error, {timeout:10000});
    

    This ^ will not be executed, so gMap.init will not be executed.

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

Sidebar

Related Questions

I thought I understood sed but I guess not. I have the following two
I don't claim to know anything about svn, but I thought I understood how
I thought I understood ViewState, but this is a bit of a weird one.
I'm having a blackout here. I thought I understood these principles, but I can't
I thought I understood what the default method does to a hash... Give a
I have been trying to understand how vertex textures work, but do not understand
I thought, I understood unicode and python. But this issue confuses me a lot.
I really thought I understood Python variable referencing, so I'm confused why this code
Being a newbie I thought I understood what to do from a security standpoint
After I thought that I've understood how they work, I tried this: NSString *str1

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.