Why javascript return Uncaught ReferenceError [duplicate] - javascript

This question already has answers here:
ReferenceError and the global object
(3 answers)
Closed 7 years ago.
Why javascript throws Uncaught ReferenceError when trying to get global variable directly for example someVar, but when trying get it through window.someVar it gots undefined

Please post an example of your code. Global variables should be accessible on the window object for example:
var someValue = "foo";
function alertsomeValue(){
alert(window.someValue);
}
alertsomeValue();
//alerts "foo"

Related

Why is variable undefined in global object even though it has not been defined? [duplicate]

This question already has answers here:
ReferenceError and the global object
(3 answers)
Closed 1 year ago.
I have this code:
console.log(apple);
If I run the program, I get the following result:
console.log(apple);
^
ReferenceError: apple is not defined
This is what I expected, but if I modify the code to print the apple variable from the global object, I get undefined
console.log(global.apple);
Result:
undefined
How is this undefined? As per my understanding, this should give the ReferenceError too right? Sorry if this is a simple concept, I am trying to understand the fundamentals. Any reference would be helpful.
What you're forbidden to do is reference a standalone identifier that the interpreter cannot resolve.
Referencing a property of an object - even if the property doesn't exist - is perfectly fine. It will result in undefined, but it won't throw.
const obj = {};
console.log(obj.foo);

Why the alert is showing undefined after fred flinston? and what properties does "const" in ES6 have? [duplicate]

This question already has answers here:
console.log(myFunction()) returns undefined
(2 answers)
Closed 2 years ago.
let a = 'Fred Flinstone'; // This is a global variable
function alpha() {
alert(a);
}
alert(alpha());
why the following code displaying undefined after displaying fred flinston?
and what properties does "const" in ES6 have and what is meant by " referencing to memory " in const?
Thanks
Actually, after you use alert(alpha()),this code will do 2 steps:
1.get the return value from alpha()
2.execute the alert function
Because there is no return code from the function alpha , so it return 'undefined' and which will be print in the alert box

Is this JS bug? [duplicate]

This question already has answers here:
What causes the different behaviors between "var" and "let" when assign them a returned value of a function which throws an error
(1 answer)
Is there any difference between declared and defined variable
(1 answer)
Closed 3 years ago.
I tried to run this code.
let a = b; //I'm sure that variable b is not defined.
Uncaught ReferenceError: b is not defined
I know this code causes this error.
b = 3;
Uncaught ReferenceError: b is not defined
let b = 3
Identifier 'b' has already been declared
Now, I can't put any String , Number,Null and Everything.
And I can't define variable 'b'.
Is this Js Bug?

javascript variable's weird behaviour [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 6 years ago.
Case 1 - If I console.log(variable) before the variable declaration I get undefined. eg;
// code
console.log(a);
var a ;
// output
undefined
Case 2 - If I console.log(variable) without variable declaration I get Uncaught ReferenceError: variable is not defined.
// code
console.log(a);
// output
Uncaught ReferenceError: a is not defined
But incase of functions we can call a function before or after function definition it never give any issue. eg;
console.log(example());
function example(){
return 'test done';
}
console.log(example());
// output without any issue
Now My question is, what is the difference between undefined and not defined.
Undefined means - variable exists, but have no any stored value in it.
Not defined means - variable not declared (not exist).

Call a Javascript function stored in an object property [duplicate]

This question already has answers here:
JavaScript function aliasing doesn't seem to work
(6 answers)
Closed 8 years ago.
How come the code below doesn't work?
var x = {};
x.a = alert;
x.a('asdf'); // TypeError: Illegal invocation
Because the internals of the alert function requires that the value of this be window.
x.a.call(window,'asdf');
… will work.

Categories