When should one be used over the other and vice versa?
Im still learning but let this = {key: value}
would be for an object, let this = "string"
would be for a string. strings so far as i have learned is for text, objects are better for storing some type of data.
In what context are you encountering {} and “”? Their uses are very different so I am not sure how you can confuse the two. An example where the two seem to be interchangeable would help us understand what you need explained.
What basic research did you do about what {} and “” do? How can we clarify your basic research that you did?
This would be the context.
They both seem to do the same thing.
let globalVariable = {};
let globalVariable = “”;
The first makes an empty object. The second makes an empty string. That really isn’t much context without seeing what you are trying to do with those variables.
Those are just completly different datatypes.
That’s like asking when to use a cake over a car - I have trouble seeing a single situation where you’d be struggling between the two…
Those lines do nothing you can see though?
Behind the scenes, the JS interpreter is doing wildly different things.
Both of those global variables work, how do you know which to use?
empty object global variable = {};
empty string global variable = “”;
How do you know which is the right one to use if both work in the code?
let currentPlayButton = {};
let currentPlayButton = “”;
function animationEndHandler(evt) {
const animationName = evt.animationName;
if (animationName === "initial-fade") {
body.classList.remove("initial-fade");
showCover(currentPlayButton);
}
}
function coverClickHandler(evt) {
currentPlayButton = evt.currentTarget;
body.classList.add("initial-fade");
}
There is a massive difference between a string and an object. What do you want to do with your global variable? Data types and structures follow from function.
I know you are firmly against the idea, but I really think that you need to take a basic Javascript course. I don’t understand why you won’t do some foundational education. You can’t effectively create a complex piece of code if you don’t understand the difference between a string and an object.
I have trouble seeing a single situation where you’d be struggling between the two…
If both work in the code.
They won’t both work the same way.
You need to learn the basics or you will make buggy, broken code.
I am not seeing a difference.
When the svg’s are clicked, they fade out.
Which is the expected behavior.
let currentPlayButton = {};
https://jsfiddle.net/cq1eu952/
let currentPlayButton = "";
https://jsfiddle.net/xcjg5sza/
Ok. There is a difference, but you are coding a complex application without foundational knowledge, so it is hard to back out the differences and bugs. I am honestly amazed that your code runs since you don’t know how a string and an object differ. There isn’t a lot that can be done to help if you are uninterested in actually learning the basics. Good luck with understanding your code and continuing your project.
I was just told they are both fine.
No one has outright said it yet, but those absolutely do NOT do the same thing under the hood. While the code you posted might run, and appears to run in both JSFiddle versions, it actually doesn’t.
If you take the time to actually trace through the code, you can plainly see that one version will not work. It will be super, ultra obvious why.
Additionally, you didn’t say who wrote the code in both JSFiddles but it’s written and organized extremely poorly, and needs to be drastically re-structured. As it is now, I’d call it a nightmare to maintain.
So you created a situation where the datatype of the variable doesn’t come into play at all… as in, you could write whatever you want there. You can write a number or nothing - and the code will still run.
Seriously, you override the currentPlayButton
later on, so ofcourse the initial value doesn’t matter. JS doesn’t have strict typing and will just change the datatype of the variable if needed - without throwing an error.
It literally needs a special comparison-operator to also check typing because it’s changing it on the fly.
So what you are doing is quite troublesome. You are using objects, methods and functions in a somewhat complex structure, but you lack basic knowledge about JS. Did you write that code or copy it from somewhere?
@astv99 just told me it does matter, now you are telling me it doesn’t matter.
Now I’m confused.
He said this:
those absolutely do NOT do the same thing under the hood. While the code you posted might run, and appears to run in both JSFiddle versions, it actually doesn’t.
Then go and look up the difference inbetween a string and an object, namely JSON (JS object-notification).
Ofcourse it does matter that you know the difference.
However if you override a value, it DOESN’T matter what it was before.
You can declare a variable as a number and then override it with a string, then a function, then another number again.
The moment you override something, the previous value is lost. At that point it no longer matters what it was. It’s like burning a book - be it a dictionairy or a diary, once burned, it’s ash. However you still should know the difference between the two, in case you are intending to not burn them.
You are assigning one type of thing to a variable, then you are overwriting it with something else completely different. That’s why they’re saying it doesn’t matter. They are not saying it doesn’t matter because the types of things are in any way comparable.
If you initially assign something completely unrelated to that variable, in this particular piece of code it isn’t going to do anything apart from be extremely confusing to read. Why are you assigning something unrelated to it? There is no reason to do that, and as others have said seems to indicate very large gaps in your knowledge of very basic programming.
If you don’t know what it is in advance, then why are you not doing this?
let currentPlayButton;
// Rest of code
currentPlayButton = thingYouAreAssigning
Is there a name for what doing this is called?
let currentPlayButton;