let vs var in the global scope [duplicate] - javascript

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 5 years ago.
Why this will print the names as expected:
var firstName = 'Charlie',
lastName = "Brown"
function showFullName() {
console.log(this.firstName+' '+this.lastName);
}
window.showFullName();
but, if I replace 'var' for 'let', I will get 'undefined' for both firstName & lastName?
I also see that the variables declared using 'var' get attached to the window Object, but the ones declare using 'let'.
Appreciate the help.

Let allows to declare variables which are limited to a particular scope, can be block or expression. Where as var is used for global or local declaration. We can use var instead of let but the reverse fails.

Related

In JavaScript; does a let variable declared outside of a block get a global scope? [duplicate]

This question already has answers here:
Do let statements create properties on the global object?
(5 answers)
A javascript 'let' global variable is not a property of 'window' unlike a global 'var' [duplicate]
(1 answer)
Closed 2 years ago.
In JavaScript; does a let variable declared outside of a block become a global scope?
let letVariable = 2;
console.log(letVariable);
I don't know if I understand you correctly.
There is no "outside of a block", because even if you use it on the blank page, it is in the "main/global block" but only in the same script. its not available from other scripts.

What is the scope of my variable when I use let? [duplicate]

This question already has answers here:
Do let statements create properties on the global object?
(5 answers)
A javascript 'let' global variable is not a property of 'window' unlike a global 'var' [duplicate]
(1 answer)
What is the difference between "let" and "var"?
(39 answers)
Closed 2 years ago.
Im new in javascript and in programming.
If I declare a variable with var I see that my variable is declared globally and my variable is inside the window object.
Example:
var element1 = 1;
window.element1; //This returns 1
But when I use let I can't access my variable using the window object.
Example:
let element2 = 1;
window.element2; //This returns undefined
So, where has been my variable element2 been declared?
What is the scope of element2 ?
You are confusing scope and automatic attachment to the default object.
If you use let or var outside of any block, function, or module then the scope will be global.
var will also attach a property of the same name to the default object (which is window in the case of JS running in a browser).

Declaring a variable that isn't var, const or let [duplicate]

This question already has answers here:
Is using 'var' to declare variables optional? [duplicate]
(14 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I'm refreshing my JS knowledge by doing a JS course. There's something about variables that have left me a little confused.
var variableVar = 'This is a variable pre ES6'; // var
let variableLet = 'This is a variable that can be changed'; // let
const variableConst = 'This variable cannot be changed'; // con
alsoWorks = 'This also works'; // What's this?
console.log(variableVar);
console.log(variableLet);
console.log(variableConst);
console.log(alsoWorks);
I understand var, const and let. I also noticed I don't need to declare any of these for a variable to work e.g. alsoWorks = 'This also works';. Why shouldn't I write a variable like this?

Logging Global variable inside the function Block immediately before redeclaration going undefined: Should not it take the Global Variable Value? [duplicate]

This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
Closed 5 years ago.
let pages =100;
let pageFunction=()=>{
console.log(pages);
const pages = "20";
//console.log(pages);
}
pageFunction();
Output:
Uncaught ReferenceError: pages is not defined
at pageF (:3:13)
at :1:1
According to Mozilla "The let statement declares a block scope local variable, optionally initializing it to a value." Also "At the top level of programs and functions, let, unlike var, does not create a property on the global object."
See
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/let for more information.
This means that in the code you gave you effectively create an implicit global variable pages, as let pages is not really global, but restricted to the scope it is in.

Javascript: this.varName = "" vs var varName = "" [duplicate]

This question already has answers here:
Difference between this and var in a function
(5 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 9 years ago.
I'm new to JS so noob question:
I see functions which define vars inside them:
function functionName(){
this.something = "";
};
If I understand correctly, something is a local variable? Why is it being defined as this.something = '' as opposed to var something = ''?
Is there any difference, and if so what is it?
It sets the attribute something of this. What this refers to depends on how functionName is invoked, but typically it's an object created with new:
var foo = new functionName();
console.log(foo.something);
If you'd use var something inside the function, the variable would be inaccessible from outside, so you couldn't do foo.something in the above example.
var someThing
is a local variable - meaning it exists within the scope of the current block
this.someThing
is an instance variable - meaning it belongs to the object and is visible in all methods of that object.

Categories

Resources