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

  • Home
  • SEARCH
  • 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 6642805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:58:26+00:00 2026-05-25T23:58:26+00:00

As a test to make sure calls to the JSON object don’t fail in

  • 0

As a test to make sure calls to the JSON object don’t fail in IE I recently added this to my js library:

//Works
//if(!JSON) var JSON={};
//JSON.stringify = JSON.stringify || function(){};
//Works
//if(!window.JSON) JSON={};
//JSON.stringify = JSON.stringify || function(){};
//Does not work
//if(!JSON) JSON={};
//JSON.stringify = JSON.stringify || function(){};
//Best of both!
if(!window.JSON) var JSON={};
JSON.stringify = JSON.stringify || function(){};

When I say “does not work” I mean it doesn’t work in IE7 and throws a “JSON blah blah blah” error. Why does specifying window.JSON not throw the error? And why would not specifying window.JSON but using a var JSON not throw an error? Is this just silly IE stuff I shouldn’t worry about or is the result of this dangerous and going to be a problem for other browsers?

Note the same is true for “console”.

  • 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-25T23:58:26+00:00Added an answer on May 25, 2026 at 11:58 pm

    Accessing an undefined global variable will always throw. Accessing an undefined property of an object will always return undefined. The fact that window is an alias for global doesn’t change the rules here, accessing a undefined member of window will return undefined.

    As for why the second example doesn’t throw is because all vars are hoisted to the enclosing scope regardless of where they appear and before any code is executed in that scope. This means the JSON variable is defined even if the var is not executed, it will just contain undefined. var does two things, creates a variable in scope and, optionally, intializes the variable. Creating the variable is hoisted to the top of the scope, assigning it is performed where it appears. All variable contain undefined until they are assigned a value. If the assignment is not executed they will remain undefined. Duplicate var in the same scope are ignored. This means that if JSON is already in scope, the var is ignored.

    This is not silly IE stuff, all ES5 conforming JavaScript implementation need to follow the rules I outlined above.

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

Sidebar

Related Questions

I'd like to make sure a given view in my test is fetching an
I have been trying to test my application to make sure that all the
How can I make a verify error for this command? if blablablabla: os.makedirs('C:\\test\\') If
Suppose this is my URL route: (r'^test/?$','hello.life.views.test'), How do I make it so that
I need a unit test to make sure I am accumulating vacation hours properly.
I don't understand this at all. Here is some Javascript code that works in
I am trying to make a Test Webservice and throw a SoapException. But when
If I make an empty test class: public class Foo { } And I
I am trying to make a boolean test so that if one of the
Is there a way to make running junit test to stop after a test

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.