are Javascript const variables really constant? [duplicate] - javascript

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.

Related

Accessing variable value by string name [duplicate]

This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
How can I access local scope dynamically in javascript?
(4 answers)
Closed 3 years ago.
Issue: I want to access some variables I got in my js-file by a string.
I know there is an option to receive variable value if you have an object and pass the object key name in square brackets to the object, but this is not the case here. Here I have just one comon variable, and I want its value by "sending" a string down.
Example on what I want to achieve:
const constantName = 12345; // Value to access
const stringToUseToGetTheValueOfConstantName = 'constantName';
// I want to know how to get the '12345' by something like this:
const valueFinallyAccessed = `${stringToUseToGetTheValueOfConstantName}`; // I know this returns a string
console.log(valueFinallyAccessed); // 12345
Solution
const valueFinallyAccessed = eval(stringToUseToGetTheValueOfConstantName);
console.log(valueFinallyAccessed); // Prints out the value 12345

Is it possible to create dynamic keys in a object in javascript? [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 5 years ago.
for example I ask the user to enter an input and he enters "key" and I save the value on a variable called INPUT.
Then I want to create an object with this like this.
var obj = {INPUT: "some other input"}; where INPUT = key
I know I can add more values but I need to know the key ahead of time in order to add it. Can I add a new key without knowing what it is?
Yes, with computed property names. Assuming INPUT is an actual variable, simply wrap it in [].
var obj = {[INPUT]: "some other input"};
The long version of doing this would be to use bracket notation when adding your keys.
var obj = {};
obj[INPUT] = "some other input";

JS: can a var/object declared as const be deleted [duplicate]

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.

Javascript: How to use an object's attribute if I have it like text [duplicate]

This question already has answers here:
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 8 years ago.
I have a path to an objects attribute in text, and I need to set some value on that path, but on the real object
Example:
var path = 'object.children.child[0].id';
var id = 1;
And I need to set it on the id on the attribute located on path
Use eval(), it interprets strings as Javascript code. People say it's unsecure, but it really depends on how you expose it.

Dynamically assign name to object array [duplicate]

This question already has answers here:
How do I loop through or enumerate a JavaScript object?
(48 answers)
Closed 10 years ago.
I have a javascript issue.
If I have an object array objAr, the object consists of id,name.
If I was to access objAr[0].id it returns the id value of the first object. What would happen if the object is dynamic and therefore I do not know what it consists of, is there a way to dynamically call the Object attribute?
Currently I am creating another array
var theArr = new Array("id", "name");
and call:
objAr[0].theArr[0] instead of objAr[0].id.
Is there a way to do this better using Javascript?
With Javascript you can call all of the attributes in an object without knowing the keys.
See below:
for(key in objAr[0]) {
console.log(objAr[0][key]);
}
If you just wanted the first attribute you could run:
for(key in objAr[0]) {
var attFirst = objAr[0][key];
break;
}
Additionally for the JS array you could have used square brackets.
var theArr = ["id", "name"];
hope that helps
In javascript you can always use the "array notation" in place of the "dot notation"
So these 2 lines are the same
objAr[0].id
objAr[0]["id"]

Categories

Resources