Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught Reference Error in Javascript & undefined [duplicate]

Tags:

javascript

Consider:

var myString = new String("Hello world");
alert(myString.noSuchValue); // undefined

alert(myshit);  // This results in error, and not the above one

Both of them, i.e.

  • property noSuchValue on object myString,
  • variable myshit

are undefined.

But why do I get an error for myshit and not for the object property case?

enter image description here

like image 763
Farhan Shirgill Ansari Avatar asked Apr 17 '26 18:04

Farhan Shirgill Ansari


1 Answers

You can obtain the proper answer from the ECMAScript specs, but I'll summarize it in the following example:

var data= myObj.prop1;

The expected behavior is:

  • if myObj (the base reference) does not exist, throw a ReferenceError.
  • if the base exists, but the property not, return undefined
like image 85
Pablo Lozano Avatar answered Apr 20 '26 06:04

Pablo Lozano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!