DEV Community

Tricky JavaScript Questions

Tomer Raitz on October 07, 2020

As part of my journey to become a better JavaScript programmer, I need to dive deep into JavaScript foundations. In this article, I'll show you min...
Collapse
 
alexparra profile image
Alex Parra • Edited

Nice round up. Great to refresh some tricky aspects.

In Q9, I’d like to clarify that let and const are also hoisted but can’t be accessed before declaration due to what is called Temporal Dead Zone.

Ref: jsrocks.org/2015/01/temporal-dead-...

Collapse
 
zaunermax profile image
Maximilian Zauner

very interesting read, thanks!

Collapse
 
tomeraitz profile image
Tomer Raitz

Thanks :)

Collapse
 
tomeraitz profile image
Tomer Raitz

Thank you for the response, I didn't know it, I thought const and let are not hoisted, but your explanation makes more sense.

Collapse
 
satyam_prg profile image
Satyam Jaiswal

Thanks for Sharing. Also learn top JavaScript Tricky Interview questions

Collapse
 
ignore_you profile image
Alex

At first, thanks for all the examples and explanations! When I was taking this quiz, a thought "who ever write code this way?" appeared too many times :) I think these questions are good as a tricksters for A-graders, but if this kind of code somehow had appeared in PR, I would have never approved it

Collapse
 
tomeraitz profile image
Tomer Raitz

Thank you for the feedback, but can you elaborate more on your response? These questions were made for developers to strengthen their knowledge (even if you know the answer). The examples were made to be very simple, only to understanding the concept behind the question.

Collapse
 
ignore_you profile image
Alex

Well, my message wasn't about all the questions, most of them cover underlying concept and doesn't contradict to real world. But examples about hoisting or array reference looks like smelly code. Though I didn't check with my IDE and SonarLint that examples, I'm almost sure I'll get warnings.
P.S. english is not my native lang, so I'm bot sure 'bout the tone of voice of my message, hope it's not seems offensive or smth

Thread Thread
 
tomeraitz profile image
Tomer Raitz

On the contrary, I glad to hear feedback, even if it negative. I will take your tips to my attention. Thanks

Collapse
 
ninjasun profile image
ninjasun

Hi, on number 3 I do not think definition of clousure is correct. Because a closure is not a function that return another function. As per your example is the variable value that gets a reference to param 2. So the variable get close inside the scope and by that the function cant return. In your example the closure refers to value.

Collapse
 
tomeraitz profile image
Tomer Raitz

I'm not sure you are correct, And I didn't understand completely what you mean. But, I think we say the same thing. I believe it doesn't matter if you know the exact definition of closure "by the book", the most important thing it's to understand the concept.

Collapse
 
eladh profile image
Elad Hirsch

10x mate :-) , it's always important to refresh the basics.
In Q8 - Running the code in Node will result undefined , see more details :
stackoverflow.com/questions/602986...

Collapse
 
tomeraitz profile image
Tomer Raitz

Thank you for the response. It's is very helpful. You right, The window object exists only in the browser (client-side). I wanted to show the basic concepts of javascript. The scope of node js it's a little bit different. You can create a global variable by doing global.length (not recommended) - but without it, I think this won't know what length is. if you that in node.js it will work:

global.length = 10;
function fn () {
  console.log (this.length);
}
fn();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
starboysharma profile image
Pankaj Sharma

Thank you so much for this quiz. I have scored 7/10 and last two questions help me to understand hoisting in more clear way. 🤯

Collapse
 
blackr1234 profile image
blackr1234

Very useful questions that help me revise my JavaScript knowledge. Thank you!

Collapse
 
tomeraitz profile image
Tomer Raitz

Very happy to hear. Thank you for the comment

Collapse
 
sorinfratila profile image
sorinfratila

Answer to nr 5 is wrong though. It is [1,2,3] because in the function argument you pass the same referrence that you are logging.

Collapse
 
sorinfratila profile image
sorinfratila

Aa dude, my bad, the answer is actually corect, but the anchor takes me to the answer to Q6 and I didn’t realise.

Collapse
 
sahilatahar profile image
Sahil Atahar

Nice brother. I was also confused when you said hoisting only works for var and then asked to ChatGPT and see comments, which helped me to understand. You should update that hosting part.

Collapse
 
tomeraitz profile image
Tomer Raitz

Thank you! I glad it helped you. And thank you for sharing the video.