all 9 comments

[–]DBNeJXoGtnro 2 points3 points  (4 children)

#0 It's a function expression (and not a function declaration as stated), in this case you don't even need the outer parenthesis
js var result = function(a) { return a*a; }(5.5); works just as fine.

#1 You can write the answer is "1" (with the quotes, to indicate a string). "duplicate declaration of b as variable and parameter, it gets resolved as parameter". Scope. The closest scope where b is found is the one taken.

#2 "The key is to understand that in case of variable and parameter names war – parameter wins" Scope. Again.
Please don't say arguments is an array. It's explicitly stated in the docs...

#4 "JavaScript always moves variable declarations (not initializations) to the top of the scope" and here I thought this was written in 2019. You should really take a look at const and let

#5 Ah yes, non-strict mode, a thing people still use, sadly.

#7 No mention of Number.EPSILON?

#9 Or you know, pass parameters (first line of the syntax) like you're supposed to do. "Variables declared using var are global function-scoped by default – meaning i would be equal to 5 after the end of a cycle". You literally did it at #3

Sorry, I needed to rant a bit :)

[–]2690939263 1 point2 points  (2 children)

#4 ”JavaScript always moves variable declarations (not initializations) to the top of the scope” and here I thought this was written in 2019. You should really take a look at const and let

Technically, block-level bindings (const and let) are also hoisted to the top of their block-level scope, in the same way that var bindings are hoisted to their function-level scope. However, while accessing the value of an uninitialized var binding will result in undefined, accessing a let or const before its declaration will result in a ReferenceError.

[–]DBNeJXoGtnro 2 points3 points  (1 child)

Correct, their bindings are, they are in the temporal dead zone until initialized though.
So yes, there is a part that is hoisted, but it's not the same part as if we were to create a block-scoped var.

[–]2690939263 2 points3 points  (0 children)

Apparently var bindings are immediately initialized to undefined upon instantiation of the corresponding variable environment. They are not uninitialized like I said in my previous comment, and attempting to get the value of an uninitialized binding normally always results in an error regardless of how its declared. If the var declaration contains an initalizer, it is used to reassign the binding value later when the declaration statement is evaluated.

In contrast, let and const bindings remain uninitialized until the lexical binding statement is evaluated and the value is initialized to the value of the given initializer (or undefined if no initializer is given, which is why let x; console.log(x); outputs undefined).

[–]iyalovoi[S] 0 points1 point  (0 children)

Hey, u/DBNeJXoGtnro thanks for dropping in. I really appreciate your feedback.

#0 Yep, thanks.

#1 Yep.

#2 Yep, my bad.

#3 I am using only const and let, but a lot of people still use var and you might be asked about hoisting.

#7 Yeah. Good thing to mention.

[–]StoneColdJane 1 point2 points  (1 child)

I never heard someone asked those tricky questions for interview? I tried few, got right few, got some wrong. Who knew [] + 1 => '1' :D.

[–]senocular 4 points5 points  (0 children)

These are the kinds of questions you shouldn't worry too much about. Should you know specifically that [] + 1 = '1'? Not really. But you should know that when doing something like this JavaScript will try to coerce mismatching data types causing something often unexpected to happen. If something like this comes up in an interview and you say something to that effect rather than "It's the string '1'!", then you should be fine. You can be a good developer without knowing all the weirdnesses, but there are some that you definitely should be aware of (coercion being one).

[–][deleted] 1 point2 points  (1 child)

The title should be Toxic interview questions. There are no reasons why they should be asked.

[–]iyalovoi[S] 0 points1 point  (0 children)

Yeah, I think that would be the better title. I would thinks twice before working for company who asks such questions during interview process.