Hello! I feel I’m failing to understand a concept of variables. That is the idea that you could somehow change what’s inside a variable using another variable using the let. EX:
let mass = 1;
const earthGravity = mass * 6;
console.log('Earth has a gravity of ' + earthGravity + '.');
//Output: Earth has a gravity of 6.
mass = 2;
console.log("If you double Earth's mass it will have a gravity of " + earthGravity + '.');
//Output: Earth has a gravity of 6. No change.
I realize now that this is a backwards way to do things when i could just use the mathematical assignment operators, but the question still stands as to why changing mass to 2 changes nothing inside earthGravity variable. Is it because I’ve placed a variable within a variable? Is that poor programing? I’m at a loss.
It has to do with earthGravity having a persistent binding to the original value, 6, which cannot be changed. mass may have changed, but it will have no effect on earthGravity.
OHHH i think i get it. The variable is originaly 1 * 6, so earthGravity IS 6, changing the mass doesn’t somehow make the framework re-read the variable as it’s already been declared as 1*6. Defiantly gonna stick with Mathematical operators when trying to manipulate variables.
Exactly, especially since the value is a singular number, akin to primitive.
const declares an immutable binding on its defined value. If the value is an object or array, the binding is to the object, but not its properties or elements.
const a = []
We can add and remove or change (mutate) the contents of the array but cannot change its type or assign a new array.
const b = {}
Likewise, we can add and remove or change properties of the object but not its type, nor assign a new object.
What is a template literal? Please use basic definitions wherever possible. I do know the definition of ‘literal’, but i’m finding it difficult to wrap my mind around the definition of ‘template literal’ (of which i also understand that it is also synonymous to ‘string literal’. Correct me if i am wrong). I do understand that they are alternatives for concatenation. Please help.
How do I cut short the the length of my coding as shown in the line of codes I’ve been experimenting with as seen here?
let test1; let test2; let test3; let test4; let test5; test1 = 'example'; test2 = 'usage'; test3 = 'me'; test4 = 'I'; test5 = `The reason ${test4} am using this ${test1} is for ${test3} to fully understand the ${test2} of this syntax.`;`
console.log(test5); // prints 'The reason I am using this example is for me to fully understand the usage of this syntax.' test3 = 'her'; // I've now declared and changed the properties of test3 to her. console.log(test5); //in this log, test3 still prints as 'me' even after I've change it to 'her'.
after trying a few times, I’ve found out that the properties of test3 changes to ‘her’ after I’ve copied and pasted test5 below test3 = ‘her’;. As seen in the example below.
let test1; let test2; let test3; let test4; let test5; test1 = 'example'; test2 = 'usage'; test3 = 'me'; test4 = 'I'; test5 = `The reason ${test4} am using this ${test1} is for ${test3} to fully understand the ${test2} of this syntax.`;`
console.log(test5); // test3 prints as 'me'. test3 = 'her'; test5 = `The reason ${test4} am using this ${test1} is for ${test3} to fully understand the ${test2} of this syntax.`;`
console.log(test5); // here test3 prints as 'her'.
I’m confused as to why it only prints ‘her’ after I’ve copied the exact same string interpolation in test5 on a new line after test3 = ‘her’;? Why doesn’t the latter changes in properties of the variable overwrites the previous properties of the variables? yet it only works after I’ve “technically” changed the properties of test5 by “remaining the same”? What can I do for it to print the latter changes of variables without having to re-paste the same string interpolation in test5 for the changes to take effect?
Does that mean that the strings from the initial test variables (1 till 4) including the string interpolation of test5 had became an independent construct after test 5 was declared? So that any latter changes (test3 = 'her') wouldn’t alter the string interpolations in test5, as the initial declaration of test5 is already a construct with the immutable strings from the other test variables?
If it has become a construct, why would declaring test5 again after changing the properties of the test variables as shown in my previous post change the output of console.log(test5), yet keeping the other test variables the same rather than being “undefined” as the new declaration should be a new construct in itself.
if the construct would recognize the prior strings from the previous test variables, why does it not recognize any latter changes until test5(unchanged) is declared again?