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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:01:05+00:00 2026-06-11T14:01:05+00:00

Why when I try to access a variable that don’t exist, javascript throw an

  • 0

Why when I try to access a variable that don’t exist, javascript throw an exception but when I try to access a property that don’t exist in an object, javascript returns an undefined value?

For example, this case returns an undefined value:

function Foo(){
    console.log(this.bar);
}

Foo();

But, in this other example, javascript throw an exception:

function Foo(){
    console.log(bar);
}

Foo();

ReferenceError: bar is not defined

  • 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-11T14:01:06+00:00Added an answer on June 11, 2026 at 2:01 pm

    As every object in JavaScript is a dictionary, also known as map in other languages, this.bar is equivalent to this['bar'], i.e. access to a value by a key. In most languages, e.g. in Java, returning null, NULL, or undefined allows you to conditionally create this slot if it does not exist yet, without dealing with exceptions or any other side effects.

    However, when you just write console.log(bar) without specifying a context for the bar, it would be impossible to create a reasonable pattern where returning undefined would have a semantic meaning. There are multiple contexts, some of them dynamic, like global context window in the browser, where the bar might be defined during the runtime, so it cannot be a compile time error (as opposed to Java or C++). Hence runtime exception is thrown when variable name cannot be resolved.

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

Sidebar

Related Questions

I try to access the MainContentBlock control from the aspx, but unable to do
When I try to access info that is not presented in xml like so:
We know that if we try to access a nonexistent key of std::map with
My app crash when I try to access any property of my NSDecimalNumber amount_before_current_year:
I try to access session variables stored on a different page. When I have
When try to access site i created after 3 login attempts it gives unauthorised
When I try to access my site, then check my Heroku logs, I see
Every time I try to access my localhost/phpmyadmin it gives me this error --->
When you try to access a key which isn't in a Dictionary (for example),
The error message I gen when I try to access the web page server

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.