Let variable declared but not defined Javascript [duplicate] - javascript

This question already has answers here:
Chrome console already declared variables throw undefined reference errors for let
(2 answers)
Is there any difference between declared and defined variable
(1 answer)
Closed 3 years ago.
I have trouble understand the let declaration in javascript.
When I assign the variable to something syntactical incorrect ( and give error), it still declare the variable. But I can not change value or access it. For example:
let test = abc; //reference error: abc not defined. Expected
let test = 5; //syntax error: test already "declare"
test = 5; //reference error: test not defined
alert(test); //reference error: test not defined
So even when the code occurs error, the variable is still declared and unchangeable?

The first let test tries to create a globally scoped variable name test.
The interpreter sees the request for a new variable and creates room for it, sees the name of the variable and labels it, then sees the initial value and goes, "Wait a minute: abc is not a thing!" and ignores the assignment.
You can think of let varname = value; as actually two commands together:
let varname;
then
let varname = value;
You can test this by seeing that let the_var; in your console will instantiate a variable with an undefined value called the_var.

Related

In Javascript, how does the window object see property values before the property is assigned a value? [duplicate]

This question already has answers here:
Is Chrome’s JavaScript console lazy about evaluating objects?
(7 answers)
Weird behavior with objects & console.log [duplicate]
(2 answers)
Closed 1 year ago.
From my understanding, all properties created are given the value of undefined after the execution phase of the Execution Context - then, during the creation phase, the JS engine goes through the script, line by line, and assigns a value if it finds an = operator. With this in mind:
console.log(window);
var myVar = 1;
From the above snippet, why does the myVar property, within the global object, show a value of 1? I would have thought it would show a value of undefined, as this is what it was set to during the execution phase? If I try to access the property directly, like:
console.log(window.myVar);
var myVar = 1;
I DO see the value as undefined... it's only when I log the entire global object I'm seeing the value as 1. Am I missing something here?
Note - this is simply for learning purposes.
this has nothing to do with the window object, this is called hoisting
Hoisting refers to the process whereby the interpreter appears to move the declaration of functions, variables or classes to the top of their scope, prior to execution of the code.
another example would be
function test(){
console.log(myVar);
var myVar = 1;
}
test()
this would still log myVar as undefined. while omitting the myVar would throw an Exception..
Edit:
this only happens in the chrome console, it would be undefined otherwise.

JavaScript hoisting confusion regarding let [duplicate]

This question already has answers here:
What is the temporal dead zone?
(3 answers)
Closed 2 years ago.
a= 10; console.log(a); let a; => Cannot access 'a' before initialization
above code gives error for initialization, even if a is declared before use. Please explain how this gives an error if a is already hoisted and get declared "a=10" before using it.
That also means if a variable is declared with let or const, hoisting doesn't have any practical impact ?
if I run below code
let a; console.log(a); => undefined
in this case I am able to print a i.e. undefined, without initializing it.
What is happening in core ?
When you type:
a=10
then you are defining a global variable. It is deprecated and not recommended style of coding in almost all languages.
To prevent declaring global variables es6 introduced let and const.
Now when you are using let or const your interpreter checks that when you typing
let a;
you do not have a used before this declaration. This is the reason for the error.
Update:
You typed:
let a; console.log(a); => undefined
it is ok because a is declared, memory is reserved, the symbol a is registered, but the value is not assigned, so when you evaluate the value assigned to symbol a it is undefined.
Please compare it with case:
console.log(c);
You will get an error:
Uncaught ReferenceError: c is not defined
because the symbol c was never declared.
Now when you will assign values to a it will be done in local scope, but when you will assign values to c it will create global variables.
To improve your understanding please read about all methods of defining variables and look at all edge cases. A great article is under the link:
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/

Why global let variables not added to window object of browser? [duplicate]

This question already has answers here:
why don't const and let statements get defined on the window object [duplicate]
(2 answers)
Closed 3 years ago.
Consider the code below which I am running in my chrome browser console:
var x = "hello";
let foo = function(){
console.log("hi");
};
console.log(x); // hello
console.log(foo()); //hi
console.log(window.x); // hello
console.log(window.foo()); // Error
If I had used var foo =... instead of let foo = .. then this would have worked. Why ?
This is by design:
let allows you to declare variables that are limited to a scope of a block statement, or expression on which it is used, unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope. The other difference between var and let is that the latter is initialized to a value only when a parser evaluates it (see below).
Just like const the let does not create properties of the window
object when declared globally (in the top-most scope).

Why can I declare a javascript var multiple times? [duplicate]

This question already has answers here:
Declaring a Javascript variable twice in same scope - Is it an issue?
(5 answers)
Closed 4 years ago.
Why am I able to declare a var multiple times? I would expect an error.
This code raises an error:
let a = true;
let a = false;
Uncaught SyntaxError: Identifier 'a' has already been declared
Why doesn't this raise an error too?
var b = true;
var b = false;
Expected: Uncaught SyntaxError: Identifier 'b' has already been declared
It's because there's variable hoisting with var, but not with let (or const for that matter).
So that means that each time you use var, it'll essentially cancel out the previous operations because to the JavaScript interpreter, your first code looks like:
var b;
b = true;
b = false;
But this doesn't work with let or const because let and const are block scoped, whereas var is function scoped.
var gets hoisted; duplicate variable names are ignored. To the interpreter, your second snippet looks like:
var b;
b = true;
b = false;
In contrast, let is not hoisted, so duplicate declarations are forbidden.

Why does a value become undefined when reassigned in its own scope? [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 8 years ago.
I have been playing around with some pointless logic and scopes and have noticed some strange behavior which has confused me...
var test = 1;
(function(){
console.log(test); //its 1
})();
var test = 1;
(function(){
console.log(test); //its 1
test = 2;
})();
var test = 1;
(function(){
console.log(test); //Uncaught ReferenceError: test is not defined
var test = 2;
})();
In the following examples I would of expected the last function to log out 1 until test is reassigned in that scope however it is undefined, if I remove the scoped declaration and reassign the top level test it then logs out 1 as expected.
Can anyone explain why that last examples test becomes undefined?
The last snippet is roughly equal to
var test = 1;
(function(){
var test; // it is undefined here as it is not initialized yet
console.log(test); // undefined
test = 2; // initialized
console.log(test); // hence 2
})();
due to the fact that the variable declaration is hoisted to the top of the function. So, when you log test it is undefined and is over-shadowing the test which is outside.
A really nice article explaining what happens (and many details!):
Function declarations and variable declarations are always moved (“hoisted”) invisibly to the top of their containing scope by the JavaScript interpreter.
This is why you cannot access the global variable before the declaration of the local one. This would be a weird thing to access two different variables with the same name in the same scope no?

Categories

Resources