This question already has answers here:
Why is assigning a value to the variable "let" possible? [duplicate]
(2 answers)
Why is 'let' allowed as a variable name? [duplicate]
var let is ok but let let isn't?
(5 answers)
Closed 2 years ago.
Playing in the chrome console I've found out that you can assign a variable to the keyword "let", but only using var. Is there a reason for this choice, or some bug that if fixed will break more stuff, like fixing null to not be Object?
const let = 'asd'; // Error
let let = 'asd'; // Error
But the below snippet works.
var let = () => 5;
let();
Related
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
let varone = "vartwo";
let vartwo = "answer";
And I would like to get "answer". But I cannot do the following:
console.log(vartwo);
How can I get "answer", but not by including "vartwo" in my code?
I think one way to resolve it, is by using javascript objects, as shown:
obj={
varone:"vartwo",
vartwo:"answer"
};
console.log(obj[varone]) //outputs "answer"
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?
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 4 years ago.
CODE:
let name = 'bam';
doc.name = req.body[x].value;
PROBLEM:
I want it to return doc.bam value but actually it get doc.name value while I set name='bam'.
For computed properties you should use []
var doc = {};
let name = 'bam';
doc[name] = "abc";
console.log(doc.bam);
This question already has answers here:
What is the difference between 'let' and 'const' ECMAScript 2015 (ES6)?
(10 answers)
Keyword 'const' does not make the value immutable. What does it mean?
(5 answers)
Closed 5 years ago.
I'm wondering what is the difference between let and const in ECMAScript 6. Both of them are block scoped
The difference between let and const is that once you bind a value/object to a variable using const, you can't reassign to that variable. Example:
const something = {};
something = 10; // Error.
let somethingElse = {};
somethingElse = 1000; // This is fine.
Note that const doesn't make something immutable.
const myArr = [];
myArr.push(10); // Works fine.
Probably the best way to make an object (shallowly) immutable at the moment is to use Object.freeze() on it.
This question already has answers here:
What is the global variable called 'name' in javascript? [duplicate]
(2 answers)
Using the variable "name" doesn't work with a JS object
(4 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 5 years ago.
since today I believe in ghosts.
Can someone explain the following JS behaviour with Chrome (Version 59.0.3071.115) or Firefox (54.0 (64-bit)) ?
Why?
var name = 10;
console.log(typeof name); // string
console.log(name); // "10"
Same with let works:
let name = 10;
console.log(typeof name); // number
console.log(name); // 10
What I believe is, that window.name is used and it transforms any input to string. But then, why is var name not a local variable and hides the global variable?
Thank you.