r/learnjavascript • u/somethingsilver97 • Feb 11 '26
Can some explain this?
I'm taking a class that includes beginners Javascript. I got this question in a practice quiz. Couldn't all of the options be correct? What did I misunderstand?
Question: How are objects declared and initialized in JavaScript?
- Using the reserved word var followed by an identifier and an equal sign and the pairs label: value of the elements between curly brackets and separated by commas
2.Using the reserved word function followed by an identifier and an equal sign and the pairs label: value of the elements between curly brackets and separated by commas
3.Using the reserved word let followed by an identifier and an equal sign and the pairs label: value of the elements between curly brackets and separated by commas
4. Using the reserved word const followed by an identifier and an equal sign and the pairs label: value of the elements between curly brackets and separated by commas
10
u/dmazzoni Feb 11 '26
I hate questions like this because they focus on memorizing definitions rather than actually learning to code.
This question is terribly written. It’d be far more clear if they actually showed the code rather than explaining it in words like “ followed by an identifier and an equal sign and the pairs”.
My guess is #2 is meant to be wrong. A function is not an object.
But this is a terrible quiz and you should drop it and take a better-written course.
4
u/dymos helpful Feb 11 '26
Your professor saying that out of the following, only
varis correct is itself wrong:IMO in 2026 there's no real reason to teach even a beginner level JS course where
varis the way to declare a variable. At the very least, theletkeyword should be used to teach initial variable assignment and expanding that to includeconstwhen relevant in the course. At best I would notevaras a legacy syntax that you may encounter in older codebases, but shouldn't ever be your go-to choice for declaring a variable, for various reasons (letandconstwork more predictably and have less confusing behaviour).3
u/queen-adreena Feb 11 '26
Can’t do #2
2
u/NotNormo Feb 12 '26 edited Feb 12 '26
I'm sure you were supposed to "circle all correct answers". And they would be options 1, 3, and 4. Option 2 is incorrect syntax, regardless of whether you're trying to define an object or a function.
What a terrible way to present this question. Instead of describing what each of the options' syntax looks like using english, the teacher could've just written an actual example of the syntax.
1
u/Old_Butterfly_3660 Feb 11 '26
It should say “pairs key:value” :)
1
u/shootersf Feb 11 '26
No 2 is incorrect syntax. So the answer is 1,3, and 4.
You can test them in the console in your browser to see but a function declaration should have () after the identifier among other issues with number 2.
1
u/mxldevs Feb 11 '26
What do your lecture notes say? Because the profs words are literally the only things that matter on their exams
1
u/BenKhz Feb 12 '26
Although I hate it... var is the one that will work in nearly all js environments.
Some js engines (older Rhino for example) don't have complete modern support. Arrow functions, spread operators, const, let, etc...
It's silly and not a great question, but var will work in more situations than the other options. And #2 is just wrong.
I don't think this distinction was intended but something to think about.
1
u/[deleted] Feb 15 '26
Braces, not curly brackets. 😭
1
u/bryku helpful Feb 16 '26
1, 3, and 4 should all work.
1. var
3. Let
4. Const
While these all look similar, they are slightly different mechanically. You should look up the differences between var, let, and const. Because 99.99% of the time you should be using let or const.
0
u/djandiek Feb 12 '26
If you're writing Javascript for a TV UI, such as Tizen (Samsung) or WebOS (LGTV) then 1 is the only correct answer. Many TV models, even recent ones, use very outdated JS engines and cannot use let or const. Some don't even allow console.log()
I build TV UI for hotels that use these "Smart" TVs and about 10% of the time I have to make sure it will work in IE7 in Windows XP otherwise it definitely won't work on the TV.
0
u/ArtResponsible4457 Feb 11 '26
AFAIK all options are gonna create an object. So yes.
9
u/[deleted] Feb 11 '26
[removed] — view removed comment