r/javascript • u/bhalp1 • Aug 23 '17
10 JavaScript concepts you need to know for interviews
https://dev.to/arnavaggarwal/10-javascript-concepts-you-need-to-know-for-interviews45
u/AndrewGreenh Aug 23 '17
Oh no, not the "pass by reference" discussion again! Javascript is pass by value only, while some values are references themselves, this does not mean that its pass by reference.
32
Aug 23 '17
If your interviewer asks you about "pass by reference" the right answer is to explain how parameter passing works in JavaScript. You can avoid saying the words "pass by reference" and instead say something like "when objects are passed we receive a reference to the object, so any changes we make to it will persist after the function returns" or whatever.
Correcting the interviewer on terminology probably isn't the right answer, especially since it's perfectly clear what they mean and this (mis)use of "pass by reference" is very common in the JS community.
Nobody wants to hire someone who sees every question as an opportunity to correct you.
2
u/benihana react, node Aug 24 '17
not to mention, pass by reference and pass by values are concepts that existed for decades before javascript even showed up. it might be beneficial to show you have some perspective and don't just solely focus on js.
13
u/cheese_wizard Aug 24 '17
This is pretty close. Both Java and JavaScript do what's called 'Pass by Sharing'. They pass a reference by value, which sounds weird. Basically, the objects methods and values are available through the 'reference', but overwriting the reference (like swap) will only overwrite the local object, not the one it looks like it points to, because it's not a real reference, like in C/C++.
2
u/b4ux1t3 Aug 24 '17
Does that not mean that you're essentially passing a pointer? Which is, at its core, passing by reference? The only difference between this and how C does it it, for example, is that you don't need to use
->?I'm genuinely asking, I'm not the most familiar with JavaScript.
2
u/TrylessDoer Aug 24 '17
Good point. Here's a code snippet showcasing this:
var _myObject = {hello: 'world'}; function deleteMyObject(myObject) { // Or use _myObject without passing a function arg myObject.lastFunctionCall = 'deleteMyObject'; myObject = null; // `delete` keyword doesn't work on object references, so we need to set this to null delete myObject; }; deleteMyObject(_myObject); // _myObject still exists // _myObject now equals {hello: 'world', lastFunctionCall: 'deleteMyObject'}4
u/pgrizzay Aug 23 '17
Isn't that the definition of pass by reference? (to pass a value of a reference)
5
u/HomemadeBananas Aug 23 '17
Nope. If you assign new values to your parameters, it doesn't change the original value where it was passed in to the function. In C or C++ for example that's how pass by reference works.
2
u/pgrizzay Aug 23 '17
yes, if you assign new values to the references, then those references are different. You're still passing the reference though, right? (haven't coded in c++ in a while)
3
u/HomemadeBananas Aug 23 '17
C++ has both pass by reference and pass by value.
-1
u/pgrizzay Aug 23 '17
Okay, then I suppose to appease the purists... Javascript passes primitives by value and the value of the reference for non-primitives.
It's still accurate in my mind to say javascript passes primitives by value and non-primitives by reference.
5
u/HomemadeBananas Aug 23 '17
It's wrong though, because that means something else. It passes references by value.
1
u/pgrizzay Aug 23 '17
In c++ when you pass by value, are you passing the value of a reference?
7
u/BB611 Aug 24 '17
C++ options:
Pass by value - testFunc(int val)
Pass a pointer by value (I think this is most similar to your question) - testFunc(int *val)
Pass a reference - testFunc(int &val)
2
u/SgtBlackScorp Aug 24 '17
I'm actually interested in what exactly the difference between *val and &val is. Could you elaborate?
→ More replies (0)1
u/Carnilawl Aug 24 '17
In the third case, are you passing any information beyond a memory address?
→ More replies (0)4
u/cheese_wizard Aug 24 '17
Look up 'Pass By Sharing', a subtle nuance that Java and JS both use... also known as passing a reference by value.
3
u/AndrewGreenh Aug 23 '17
Nope, imagine this: every variable in js is a bucket. When you call a function, you empty this bucket and the contents land in the bucket in your function (the function parameter) you can then take this content and modify it. By the you can never change, what item is in the bucket outside of your function. Sure, if the item is mutable, you can change it and it will change on the outside too, but you can never change, which item is in the outside bucket. In languages where pass by reference is possible, you can reset the contents of your variable. For example, c# has out parameters, this allows you to set the content of a variable that is not inside your function.
10
u/MoTTs_ Aug 23 '17
The issue here is different languages use the same words to mean different things. What JavaScript calls a reference is what C/C++ calls a pointer. So in JavaScript, when you hear "reference," think "pointer." Which means when we say "pass by reference" in JavaScript, it's like we're saying pass by pointer.
2
2
1
u/farm-fresh-js Aug 28 '17
I realize the wording I used may have been confusing for those coming from other programming languages. I've changed the article to remove discussing "pass by reference" in those exact terms. Instead, I wrote that for objects, the reference is copied and passed. Thanks for reading!
6
u/Vheissu_ Aug 24 '17
Maybe just my experience, but it seems the modern hiring process for Javascript/front-end developers is being asked questions like this:
Can you solve this complex coding puzzle on this whiteboard for me?
Write a bubble sorting algorithm
Write a Fibonacci number calculator
What's the difference between call and apply?
I rarely have been asked questions relating to the DOM or broad Javascript topics, save for maybe a handful of them. Most Javascript interviews seem to be heavily skewed towards developers with academic mindsets.
1
u/riledhel Sep 21 '17
I do javascript interviews as part of my job and I really like DOM related questions, how events work in browsers and anything that can lead to an interesting discussion about the whole stack. I think that way you can really get a sense of the experience of the other person, how much they care about the details or if they understand the bigger picture.
11
u/stratoscope Aug 24 '17 edited Sep 17 '17
Value vs. Reference — Understand that objects, arrays, and functions are copied and passed by reference. Understand that primitives are copied and passed by value.
This is way off the mark, and it seems to be a very common misconception.
There is no difference in observable behavior between how objects and primitive types are handled when passed into a function. They are treated exactly the same, both when passing arguments into functions and in = assignments.
It's best to avoid terminology like "pass by value" and pass by reference", because they lead to assumptions based on what those terms mean when talking about other languages.
Instead, let's talk about names and things, where a "thing" may be either an object or an instance of a primitive type.
When you pass an argument into a named function parameter, or when you use an = assignment, you are taking a name and making it refer to a specific thing. This is in addition to any other names that may already refer to the thing. Those don't change just because you made yet another name refer to the thing.
Also, these names are specific to each function invocation. If you call a function twice, the parameter names are spelled the same, of course, but in effect each invocation has a brand new set of names.
All of this is true whether the "thing" is an object or a primitive type.
So, why does it seem that they are so different? After all, what if we pass an object into a function and then modify the object inside the function:
var obj = { a: 1 };
fun( obj );
function fun( o ) {
o.b = 2;
}This modifies the original obj so it is now:
{ a: 1, b: 2 }So obviously, the object was passed by reference, right? Contrast that with passing a primitive value into a function and then modifying the value:
var value = 1;
fun( value );
function fun( v ) {
v = 2;
}Here the original value hasn't changed at all - it's still 1! So obviously, the primitive value was passed by value, right?
Wrong.
Look more closely at the code. In the first example we have:
o.b = 2;This adds a new property to the o object. And o is just another name for the object that is named obj outside the function. So both o and obj have changed, because they are the same thing.
The second example is quite different:
v = 2;We're not adding or modifying a property of v, we're discarding the reference that v originally held and replacing it with a new reference. So now v is a reference to the number 2, but this doesn't affect value outside the function, because we didn't do anything that affects that name.
Suppose we had done something similar in the first example, say:
o = { b: 2 };Now we're not modifying the original obj object, because we have discarded the reference that o had to it and replaced it with a new reference. o and obj no longer refer to the same thing - just like the "primitive value" case where we changed v to no longer refer to the same thing as value.
Going back to the primitive value case again, what if we tried to set a property on the value?
var value = 1;
fun( value );
function fun( v ) {
v.b = 2;
}The assignment into v.b would fail, but not because primitive values are handled differently from objects. It would fail because primitive values are immutable.
The same would be true for an immutable object:
var obj = { a: 1 };
Object.freeze( obj );
fun( obj );
function fun( o ) {
o.b = 2;
}The assignment into o.b would fail just like the previous case, but not because it's an object or a primitive value. It fails because the thing that o refers to is immutable.
The purported difference between objects ("passed by reference") and primitive types ("passed by value") is nothing more than an illusion caused by the fact that we don't write the same code when dealing with each.
Update: I wrote this long-winded explanation before I noticed that one of the comments on the article itself covers much of the same ground. But I still think it's useful to talk about "names" that refer to "things" instead of "pass by value" and "pass by reference", just to avoid mental baggage that comes from other languages that use those terms to mean something subtly different.
3
u/FormerGameDev Aug 24 '17
I have implicitly understood all of this for a long time, but was not entirely able to explain it. thank you.
2
u/farm-fresh-js Aug 28 '17
Hi, I'm the author of this article. When I wrote about value vs. reference, I meant to say that the reference is what's copied, as this is the only thing that's possible with JavaScript. I realize the confusion my wording might have caused and I've changed the article. Thanks for your input!
1
u/stratoscope Aug 28 '17
That's great, I'm glad my nitpicks were helpful. I'll revisit the article and let you know if I have anything else to complain about. ;-)
Keep up the good work!
2
13
u/Nullberri Aug 23 '17
Apply Call Bind,
Truth be told you'll use bind 99% of the time and Call and Apply are rare enough you'll probably just check mdm every time you think you might need them. Only to realize you still probably just wanted bind.
0
Aug 23 '17
[deleted]
2
u/vcamargo Aug 24 '17
Me too, neither call and apply. I've been doing Node+React for the last couple of years and I can't remember the last time I've used bind, call and apply. If an interviewer was pushing too hard this question I'd think the company would be working in this giant pre ES6 monster.
2
u/agree2cookies Aug 24 '17
Senior react developers I've seen use bind extensively... not sure how you're avoiding that.
1
u/Asmor Aug 24 '17
IE11 doesn't support arrow functions. We have a big client that won't let us drop IE11 support. So I still have to use bind. :/ And lots of other fun things I can't use at work, either.
Although I did just realize today that IE11 supports
letandconst, so... that's exciting. lol0
Aug 24 '17
[deleted]
2
u/Asmor Aug 24 '17
We don't even have node.
It was a mighty win when I got my company to adopt sass. And even then, we didn't install ruby. No. We use a c port called sassc.
Sigh...
2
u/elingeniero Aug 24 '17
I praised the lord the day node-sass came out to wrap sassc for node. It was my final ruby dependency after I had replaced vagrant with docker and I was more than happy to be able to uninstall ruby.
Your company made absolutely the right choice there, regardless of how slow to modernise they might be otherwise.
0
Aug 24 '17
[deleted]
1
u/davidstepo Aug 24 '17
Can you explain in what contexts and situations they utilize
bind? It could be some tiny lack of knowledge as I've been working on a project where people have been caught up usingbindin a component's constructor (React) way too much and didn't know about the class fields existing.Not all senior developers are created equal, though.
0
Aug 24 '17
[deleted]
2
u/davidstepo Aug 25 '17
Darn it, sorry, I replied to the wrong comment.
Thanks for your input into the topic, mate. I gotta be more careful next time.
0
Aug 24 '17
[deleted]
3
u/Magnusson Aug 24 '17
You don't need arrow functions for that, you can just use currying:
function add(a) { return function (b) { return a + b; } }You never need to use arrow functions since they're just shorthand. But this is just a more verbose version of the code in the comment you're replying to. They're both partial application.
2
u/elingeniero Aug 24 '17
It's a nice trick but I'd never use that pattern in a language where it isn't the norm (e.g. Haskell).
For a start, I now need to call add like this
add(2)(3)which I don't like, and secondly there's no easy way to constructaddwith an arbitrary number of arguments. For example, you couldn't write the following function in a curryable way without additional logic:// Call with add(1, 2, 3, 4) function add(...xs) { if (xs.length == 0) return 0; return xs[0] + add(...xs.slice(1)); } // Partial application const add3 = ...xs => add(3, ...xs);This is important because there are many functions which work like this so knowing how to do it this way is necessary. E.g.:
const max100 = ...xs => Math.max(100, ...xs);I like the functional style, but I don't think that shoe horning functional patterns into a language without the syntax to support it (i.e.
add(2)(3)vsadd 2 3is a good idea when it's not the community norm.2
u/Magnusson Aug 24 '17
I like the functional style, but I don't think that shoe horning functional patterns into a language without the syntax to support it (i.e. add(2)(3) vs add 2 3 is a good idea when it's not the community norm.
This is what ramdajs is for.
3
u/Buckwheat469 Aug 24 '17
I love explaining this. I have a coworker who was writing an annotator that would wrap a class function (babel, react). He needed to use apply to make sure the this context transferred, but some functions were also bound by the class because they would lose the this context for the class in some cases. Binding the functions would break the this context for the annotations, so we had to develop a nice way to preserve the this context so that that didn't break this.
3
Aug 24 '17
I'd also know Promises well. During my Amazon interview I was asked to write a Promise.resolve and Promise.reject from scratch
8
u/agree2cookies Aug 24 '17
My Amazon interviewer showed me what an Applicant.reject method looks like when called.
1
8
u/akujinhikari Aug 23 '17
Every interview I've had has had me do some kind of complex math and/or array manipulation: two things I'm bad at. It's always complex bullshit you'll never use in real life though, which makes it even worse, if you can't do it. I've lost out on so many jobs because of this, and it infuriates me. GIVE ME PRACTICAL SHIT TO CODE!
6
u/pentakiller19 Aug 23 '17
Can you give me an example of an interview question you received?
3
u/akujinhikari Aug 23 '17
It's not the interview questions; it's more during the "technical screen," which can be with or without someone "watching." That's in quotes, because once it was over Skype while screen sharing, and a couple of times it was while someone just waited on the phone, which was really weird, because she wasn't technical; her job was to listen to my typing basically and make sure I wasn't... fuck I don't even know - it was over the phone, so I don't know what her purpose was. Either way, if they aren't "watching" and give you 24 hours, those are usually much, much harder questions, because they know you're just going to try and find the answer online.
9
u/nathanjd Aug 24 '17
The perhaps you're applying for the wrong positions? We do algorithms and data structures in our interviews because we need good software engineers. Being able to just code a web app is not sufficient for our problem space. We have a strong brand to protect and an international development team so performance, reliability and maintainability are the primary concerns.
It's also not feasible to test a candidate's practicality in a short interview. Sure, I like to do a small data structure operation on the whiteboard but that's really something you have to find out after hiring.
I'd personally look for positions for "web ui developer" or "web ui developer intern" as that's where I started! Also, do you have a portfolio I could look at? It can go a long way towards showing your practicality. Ultimately though, programming is math and you'll need to learn those skills if you want to advance a career in web development. If you want to stay just front-end, you could limit your data structure knowledge to:
Arrays. The primary data structure you will interact with the make a web app. Think getting results from a REST API or validating a user form.
Maps. Everything in javascript is, in essence, a hash map. These will become your best friend when trying to optimize the performance of your application.
Trees. This is the most complex but necessary. Your entire web page is represented by the DOM which is a tree of nodes. You'll need to know how to efficiently traverse and modify this tree to make the page interactive.
It would be hard to imagine a day when I did not work with those data structures for front end web dev. I'd be happy to answer any questions you have.
8
u/dvlsg Aug 24 '17
I agree with most of that, but I'm not convinced that mastery of trees is required to know how to work in the front end. Especially with tools like React.
For example, if you asked me the last time I had to determine whether to use depth-first or breadth-first traversal to search for something in the DOM, my answer would be "never".
2
u/nathanjd Aug 24 '17
Fair enough, it probably is not high priority for most. Though I am on a team whose main focus is client performance so we investigate and write tools for our devs to help them better understand how the dom updates and cpu time underneath React work. Trusting the DOM diffing engine to do the right thing can only take you so far. Particularly if you have nodes being marked as dirty too high up in the tree which is the most common pitfall I see.
3
u/wavefunctionp Aug 24 '17
It's been my experience that people don't look at your portfolio. I've had maybe half a dozen clear hits on my portfolio website from potential employers.
You either make it past the resume screening and get a phone interview to talk about your experience/projects or you hear nothing.
This over a hundred applications over 8 months in a 4 state area. Basically anything that doesn't say senior developer, and few that do but match very closely with my skills, but not total experience. This landed dozen or so calls from HR, about half that from an actual technical lead, and a couple of technical interviews. You never get feedback even when requested.
I don't where people get this notion that there is a shortage of developers. If there were, I'd be getting more calls, the salaries would be higher, and the requirements would be lower.
2
u/nathanjd Aug 24 '17
For me personally, a portfolio is the most important piece. Even better if they've been active on github. However, I don't come into the interview loop until rather late in the process, after tech screenings. So I can't speak too much to that first layer.
What I can confirm is that it is hard to find really good front end engineers. Recruiters often do a poor job in tech screening so they end up wasting a ton of our time on garbage candidates. In my conversations with others attempting to hire in our industry, there seems to be a large divide in the skills we're looking for.
On one side, we want strong engineers. There are plenty of these out there, fresh out of college, but most are used to... real languages. They wouldn't touch front end web with a 10ft stick and I don't blame them. The confluence of a shitty (but improving) language and varied browser ecosystem is a bit of a nightmare. And that's without mentioning the increased pay they would get doing anything but web development. In short, good engineers often lack practical experience with front-end web technologies.
On the other side, we want experience with web technologies. The amount of domain knowledge required is rather high for the web so it's often not feasible to hire a good engineer then train them up on the front end. These candidates with experience are your "script kiddies", often self taught with no engineering experience which is where I started. Unfortunately they often have problems working on a team in an enterprise environment or meeting our performance and quality targets.
Who we really want to hire is someone in the middle of these two extremes, which has proven hard to find. However, if you have a strong portfolio and can sell yourself as a candidate that possesses both, you should get more real interviews. Though I do not perform phone screens so am not sure what would be most helpful there. In general, we prefer strong CS fundamentals over experience.
3
2
u/proskillz Aug 24 '17
Couple suggestions:
watch lectures or read up on algorithms
Read Cracking the Coding Interview
Practice solving these types of problems on leetcode or hackerrank.
2
u/phpdevster Aug 24 '17
In addition to higher order functions, being able to explain pure functions and their benefits is one I've come across in every JS interview I've ever had.
1
u/farm-fresh-js Aug 28 '17
I'm the author of this article! Awesome to see that it's useful. Thanks for reading!
0
Aug 24 '17
[deleted]
19
u/RotateElectrolyte grammin' the 'puters Aug 24 '17
Right? It's like the whole problem with teachers teaching to standardized tests. It seems to me most good JS software is written by people bringing concepts from other languages - who realize they're working with a shoddy bastard child of the browser wars.
5
u/am0x Aug 24 '17
Let's say you have a seasoned software engineer going into a JavaScript interview for a job he is grossly overqualified for in terms of development experience. These are the types of questions that would throw him off. However ask him basic programming concepts and he would smoke any of the other candidates.
Learning a language is easy when you understand the fundamentals of programming. Learning the quirks are even easier.
4
u/FormerGameDev Aug 24 '17
Probably depends on the environment. If you're being hired for a Javascript specific job, they probably really want you to know Javascript. My current job started supposedly as a Javascript/HTML, but my first day, I was in C++ land (a place I hadn't been in 20 years), so that was certainly fun and entertaining. I've now worked with upwards of 7 different languages in a single day. It's been .. . interesting.
2
u/kenman Aug 24 '17
I think a counter-argument would be that sometimes you just need a butt in the seat, someone who's not going to be architecting anything soon, but more-or-less doing mundane bug fixes and smallish features.
Yes, in a perfect world with unlimited hiring capital, we'd all be hiring the cream of the crop, but in reality, sometimes it's better for the org to hire 2x $50k developers than it is to hire 1x $100k because it's more about the breadth of the work than the depth.
2
u/phoenixmatrix Aug 24 '17
While I generally agree, a great developer will adjust to the specific environment.
I see far, far too many people writing C# like they do Java (the 2 only LOOK similar, but they have subtle and extremely important differences) or JavaScript like they do C# and so on.
Languages are just tools, but they are tools with very large amounts of subtleties.
Eg: If you're writing Java, you're going to be using classes. If you're using JavaScript, you MIGHT be using classes. Understanding why it's not always the case is important: if you just blindly follow the paradigms you're used to, you'll write shit.
I don't care if someone know how to use prototypes when they code, but I still have to ask if they're aware of these things. Why? To make sure they understand why some patterns are the way they are in our code base.
3
1
u/dudeguy1234 Aug 24 '17
Any tips or resources for learning about software architecture? I've been a programmer for a long time and write code in my current position but I'd like to eventually move into software development.
1
u/slapfestnest Nov 02 '17
just to be that guy: "factoid" actually means usually-false information that is repeated so often that people just assume it's true
0
0
Aug 23 '17
[deleted]
4
u/theginger3469 Aug 23 '17
Clever and unintrusive, I like it! Those full-page modal "subscribe to our newsletter" modals are Satan.
2
0
Aug 24 '17
[deleted]
3
u/aveoon Aug 24 '17
Also as an engineering manager that does a shit ton of interviews, I also agree. There's are all basic things to understand about JS.
1
u/benihana react, node Aug 24 '17
i'm also a very experienced interviewer (i guess? i dont know i never really thought to quantify experience interviewing) and i'm wondering why you think that. the fact that there are blog posts like this that tell you what to memorize makes it a pretty bad set of questions in my opinion.
0
Aug 24 '17
[deleted]
1
0
Aug 24 '17
[deleted]
1
u/sneakpeekbot Aug 24 '17
Here's a sneak peek of /r/learnjavascript using the top posts of the year!
#1: The Entire JavaScript Language in a Single Image | 29 comments
#2: This pretty accurately describes my feelings on my JS learning journey so far. | 6 comments
#3: Github Repo with 100+ Free resources to learn Full Stack Web Development.I'm a bot, beep boop | Downvote to remove | Contact ^^me | ^^Info | ^^Opt-out
72
u/crashspringfield Aug 23 '17
interviewer: Explain
this. me: Can't I just use arrow functions?