This question already has answers here:
delete or override const variables in javascript Harmony / ECMAScript 6
(2 answers)
Closed 7 years ago.
Newbie question:
Can const objects be deleted or overwritten in js?
const a = function myFunc(){}; // some code to delete 'a' and 'myFunc()' here.
A downside to using const is that you cannot clear the variable at will (const prevents it from being changed in any way).
If the variable is not in the global scope, then the garbage collector will take care of it when it goes out of scope. If it is in the global scope, then you cannot get rid of it. The work-around is to stop using const for variables that you wish to modify.
Properties of an object, assigned to a const variable are not affected by the const declaration as the const delcaration affects the variable, not the the contents that it points to. You can use Object.freeze() or Object.seal() if you want to affect properties of an object.
Related
This question already has answers here:
When to use const with objects in JavaScript?
(6 answers)
Closed 2 years ago.
We know if we declare a variable as const we can't reassign anything to them. Then how do you justify altering certain html or css properties of a node/nodelist selected in Javascript?
<h2 class="targetHTML">Dynamically varying content A</h2>
javascript part
const nodeHTML = document.querySelector(' .targetHTML');
nodeHTML.textContent = 'Varied content B';
This changes the text which is displayed within the H2 tag. The node and whatever properties or attributes of it once stored in the const should freeze?
The const is related to what object the variable points to. The variable cannot change what object is assigned to it, but the object itself can still have its properties changed.
const is CONSTANT by reference, not by value.
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
I am new to JS. I have the following question -
Is it right to say that I can declare variables only in global scope or inside a function/method only in JS and in no other type of Object which is not function/method?
I understand that properties can be defined in any object of Javascript but variables cannot be declared everywhere.
EDIT: I might not have right terminology to explain but in simplest words I need a list of places where i can write var x = .. in JS code. Please,I am not looking for scope of a variable.
you can the variable anywhere you want at the beggining of the program in the middle or if you need one in a function you can do it there too
Variables can be declared at top-level, function parameter lists, and statements in function bodies.
Object and array literals can contain nested functions, and you can declare variables inside these functions as well. But you can't declare variables directly in object/array literals; e.g. you can't write:
const foo_array = [
let a = 1,
let b = 2
];
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.
This question already has answers here:
Does node.js have equivalent to window object in browser
(2 answers)
Closed 6 years ago.
I know with browser-run JavaScript, I could use window[varName]=value; to set global variables. I seem to remember there being a function to accomplish this in Node JS, but I'm not sure what it is.
If it helps, I'm aiming to set all the properties of an object as separate own variables.
In Node.js the global variables are stored in the global object, I think...
Then it'd be so:
global[varName] = value;
// or...
global.varName = value;
I think you're confusing something. Maybe you want to initialize a object constructor expression.
{ property: "string, or anything else" }
// this expression returns a object that can be assigned
// everywhere, but when assigned, turns a reference
If you want to get/set properties in this object you must do the same thing you were doing before, index the object with the keys [...] or ., then you can optionally assign (set) the object's property with =, else it'll be returned.
This question already has answers here:
How can I access local scope dynamically in javascript?
(4 answers)
Closed 8 years ago.
In Javascript, I'm used to being able to access variables within a known namespace "dynamically" (correct me if I'm using the wrong word here) by using the [] operator. For instance (from the global namespace):
var a = 1;
window['a']; # => 1
Or from a object-type namespace:
var a = { b: 1 };
a['b']; # => 1
And I'm familiar with the basics of how this is determined:
var a = function(){ return this['c']; };
var b = { c: 1 };
a.apply(b); # => 1;
But within a function itself, how do I access local variables I've just instantiated (or redefined) using var?
Namely, I want the following function call to return 1 but without calling a:
function(){
var a = 1;
return a;
}
You can't use window['a'] because a is defined locally, and you can't use this['a'] because this changes depending on the context from which the function is called.
In a realistic setting, I'd simply refactor to avoid dynamically creating and accessing local variables because it's generally a bad idea anyway, but as an academic question, I'm curious as to whether it's possible to access a via its string name at all.
You're mixing up local variables (which are not properties of an object) with properties (which are not local variables). There is no answer to your question, or, rather, the answer is "it can't be done".