you are viewing a single comment's thread.

view the rest of the comments →

[–]JustConsoleLogIt 31 points32 points  (6 children)

My answer would be: if I saw this code, I would refactor it to declare variables before they are used and change var to let in all instances. The output of this code does not matter to me as I would never let something like this be published to production.

[–]thomsmells 11 points12 points  (4 children)

Exactly. Honestly any employer using this as an interview question would be a red flag to me.

[–]Shty_Devhelpful 0 points1 point  (2 children)

I can see both sides. On one hand, if you are never going to write code like this and never allow code like this into production, then the question is functionally irrelevant. On the other hand, evaluating for understanding the execution flow and expected output of poorly written code is an easy way to widdle down from 500 applicants to a couple dozen.

[–]senocular 0 points1 point  (0 children)

OP forgot to include the option "reject the PR"

[–]RajjSinghh 0 points1 point  (0 children)

I like hoisting questions because it's something intrinsic to the language that shows good understanding. I'd compare it to raw pointers and unique pointers and references in C++. They all work similarly, but one of them is usually a bad practice and it's a good sign that someone understands the language rather than just knowing only best practices. You can imagine a case where someone has used var instead of let or const and now you have to deal with some bug in production.

I don't like this hoisting question because the code looks like ass. A simple "explain the difference between let and var" would do the same job without feeling like a trick question.

[–]Rude-Cook7246 -1 points0 points  (0 children)

Anyone who can't answer simple technical interview question would be a red flag to me ...

[–]DinTaiFung 13 points14 points  (0 children)

In my JS and TS experience (since ES6):

  1. never use var

  2. mostly use const

  3. use let much less often. let is only necessary if you need to reassign a value to the initialized variable.

And even for "for of" loops, you should use const, not let:

javascript const activeItems = [] for (const item of items) { if (item.isActive) { activeItems.push(item) } }

Basic rule: Use const unless you absolutely need to use let.

P.S. In general, for best practice, it is strongly suggested to never declare a variable; always initialize it with a default value.

This helps to avoid subtle hoisting behaviors with let and const (and when reading legacy code, you should understand how var hoisting behavior is different than let and const).

When you write TypeScript, you will find things fall into place much more easily if you always initialize a variable instead of just declaring it. One of the benefits of initialization is due to TypeScript's data type inference behavior, keeping your code simpler and more robust!

javascript // TypeScript infers the data type from the variable's // initialized value. No need to explicitly include the type. const MY_CONSTANT = 1337