A long list of (advanced) JavaScript questions, and their explanations
github.com
github.com
Also, that was fast. Do y'all have a script checking for Kagi/Orion mentions on HN?
And in all fairness, "stuff no one should ever need to worry about" is prime interview material for jobs. Though there's so many formats of technical interviews you never really know.
In reality, when I've seen this:
1. Shitty companies will ask you esoteric details like this that aren't relevant to your job, in which case you should avoid them anyway. It's usually from immature devs that just go down a laundry list of stuff just because it's "easier to grade". Note I'm not referring to Javascript "weirdness" that may seem esoteric but is actually pretty important to know. For example, anyone who has been programming in javascript for more than a hot minute should know `typeof null === 'object'`, because (a) nearly everyone hits this as a bug at least once and thinks "that's nutty", and then (b) if you don't know this you will be likely to write bugs in your code.
2. This also doesn't apply if you're interviewing for a job where you need to know those esoteric details, like the other commenter who replied who wrote a transpiler. But if you're just, say, a front-end web dev, yeah asking some of the nuttier details (especially all of the inherent type conversion rules) is sign of a bad company IMO.
I sure do wish I had the luxury to do that. How the times have changed so quickly.
That's pretty much why I detest the leetcode stuff that pops up even in games interviews. I'm going to be digging into decades old poorly documented legacy code to provide custom tooling for a studio's workflow. Why the hell do I need to know how to make NxN Bingo solver in Log(N) time on the fly? I guess if you gave me 20 minutes to refresh myself on dynamic progamming I could do it, but I made the "mistake" of studdying Unreal Engine/C++ trivia instead (which I've also had on other interviews), as well as real questions like what impacts and tasks I was doing at other studios.
But it's a circus out there and I just have to put on the makeup. I'm preparing long term to not have to deal with this, but I just need another 3 years or so minimum to stabilize myself and prepare my plans.
For example, I can't remember the last time I used the `var` keyword, and I had either forgotten or never really realized it had subtly different semantics compared to `let` and `const`, e.g. with respect to hoisting rules.
Ooo, there's more: https://github.com/jashkenas/coffeescript/wiki/List-of-langu...
{a:1}["a"] evaluates to ["a"]
but ({a:1}["a"]) evaluates to 1?
If that’s not correct you can provide a counter example
{ // start block statement
a: // labeled statement [1]
1 // expression statement
} // end block statement
[ // start array literal
“a” // string value
] // end array literal
vs. ( // start expression statement
{ // start object literal
a: // object literal key
1 // expression statement
} // end object literal statement
[ // begin subscript property access
“a” // string (property key)
] // end subscript property access
) // end expression
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
versusEdit: fixed second example (it did not match the parent comment), changed some verbiage
It looks bad, but it never comes up in practice because {a:1} is only parsed as a block if it's at the start of a line, which it never would be.
It’s technically correct: not all objects have prototypes (you can trivially make an object without one or remove it from an existing object).
The rest of that “answer” is just nonsense: there is no magic look up of a magic basic object. The default prototype of an object is prototype property of the creation function (e.g Object, String, MyFunction, …), and the top of the chain is Objecy.prototype unless explicitly set to something else.
There’s no magic, and I don’t know how the author got their particular explanation.
“A problem repeatedly occurred…”
(that being said, I do roll my eyes every time I have to clear out an array by doing myarray.length = 0 instead of myarray = []. JS is such a silly language sometimes.)
In your example, if myarray was the only reference to your array, it would likely not matter which way you do it. Clear the original array, or reassign the variable to point to a new empty array.
It becomes important when you have another reference to the same array.
That's not just a JavaScript thing. Any language where [] creates a new array/list, and the = operator copies a reference will have the same behavior.
Take Python for example:
>>> a = [3,2,1]
>>> b = a
>>> a
[3, 2, 1]
>>> b
[3, 2, 1]
>>> a = []
>>> a
[]
>>> b
[3, 2, 1]
>>>
>>> a = [3,2,1]
>>> b = a
>>> a
[3, 2, 1]
>>> b
[3, 2, 1]
>>> a.clear()
>>> a
[]
>>> b
[]
>>>
Or Ruby: irb(main):001:0> a = [3,2,1]
=> [3, 2, 1]
irb(main):002:0> b = a
=> [3, 2, 1]
irb(main):003:0> a
=> [3, 2, 1]
irb(main):004:0> b
=> [3, 2, 1]
irb(main):005:0> a = []
=> []
irb(main):006:0> a
=> []
irb(main):007:0> b
=> [3, 2, 1]
irb(main):008:0>
irb(main):009:0> a = [3,2,1]
=> [3, 2, 1]
irb(main):010:0> b = a
=> [3, 2, 1]
irb(main):011:0> a
=> [3, 2, 1]
irb(main):012:0> b
=> [3, 2, 1]
irb(main):013:0> a.clear()
=> []
irb(main):014:0> a
=> []
irb(main):015:0> b
=> []
irb(main):016:0>
And JavaScript to compare with the other two: > a = [3,2,1]
(3) [3, 2, 1]
> b = a
(3) [3, 2, 1]
> a = []
[]
> a
[]
> b
(3) [3, 2, 1]
> a = [3,2,1]
(3) [3, 2, 1]
> b = a
(3) [3, 2, 1]
> a.length = 0
0
> a
[]
> b
[]
Python and Ruby do have that nice clear() method instead of having to set .length to 0, but the underlying semantics are the same in all three languages.