General Javascript questions during an interview.

Preparing for an interview can be stressful, and even more so if you don't feel you've put the right preparation ahead of D-day.

By failing to prepare, you are preparing to fail.
— Benjamin Franklin

Since Javascript is both used for front-end and back-end, it's hard to overlook in the hiring process and many interviews will feature core-js questions or at least general ones.

Here are some questions you should be prepared to answer:

Q - What is JavaScript?

You didn't expect that one, right? Yeah that one can come up. It's unlikely and a bit too scholar but it can get you on the wrong path if the interviewer feels that you have no idea what JS is about from the very beginning of the conversation.

Possible answer - JavaScript is an interpreted programming language with object-oriented capabilities that allows to build interactivity into otherwise static HTML pages.

Q - What are the advantages of using JavaScript?

That's the follow-up question for the 1st one. Keep it up.

Possible answer - It allows less server interaction ; you can validate user input before sending the page off to the server. This saves server traffic, which means less load on your server.
   - It allows immediate feedback to the visitors ; they don't have to wait for an entire page to reload to see if they have forgotten to enter something.
   - You can add interactivity ; for example having an interface that reacts the user hovers over an element.


Now the more serious stuff.

Q - What is a callback?

Callback functions are really important in Javascript. JS being a event-driven language, if you write say functionA() followed by functionB() they're going to be returned in that order... unless functionA takes too long. A callback will ensure that functionB() is only triggered when the script is done with functionA().
Might not make sense right away but try replacing functionA() with puttMyClothesOn() and functionB with goToWork()...
Getting the picture?

Possible answer - In JavaScript, functions are first-class objects. One of the consequences of this fact is that functions can be passed as arguments to other functions and can also be returned by other
functions.
A function that takes other functions as arguments or returns functions as its result is called a higher-order function, and the function that is passed as an argument is called a callback function. It’s named “callback” because at some point in time it is “called back” by the higher-order function.


Michael Vollmer explains it really well in his post.

Q - What is a closure?

If you don't get that question during your interview.... well that's just not possible, you will get that question for sure. You might not get that on the very first interview but you will get it at some point. 

Closures are crucial as they allow you to create private variables. JavaScript variables can belong to the local or global scope.

Imagine you want to use a variable for counting something, and you want this counter to be available to all functions.

You could use a global variable, and a function to increase the counter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
var counter = 0;

function add() {
    counter += 1;
}

add();
add();
add();

// the counter is now equal to 3

You need a way to prevent the variable counter from being changed/altered without using the add() function but also you need the counter to be called only once if needed so it's not really a solution to modify your function with += 1 addons.

The solution is to use a closure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();

add();
add();
add();

// the counter is now 3 but it is 'protected'

The variable add is assigned the return value of a self-invoking function. The self-invoking function only runs once. It sets the counter to zero (0), and returns a function expression.

This way add becomes a function. The "wonderful" part is that it can access the counter in the parent scope.

This is called a JavaScript closure. It makes it possible for a function to have "private" variables.

The counter is protected by the scope of the anonymous function, and can only be changed using the add function.

Possible answer - A closure is a function having access to the parent scope, even after the parent function has closed. It allows to protect a variable from being changed while allowing  a script to use via a function.

 

 

Practicing is your best bet here so don't hesitate to Google for mockup interviews and tutorials on this.

Here's a great video from Codementor.io YouTube channel:

This video is great and Dan is really giving a great overview of what most screening interviews will be look like. I can't stress enough the importance of his advice here. 
You should always be communicating about what you are doing, how you are assessing the situation while you're in the coding challenge, exercise.

Remember that the interviewer is not looking for a human MDN here (check out my resources post if you don't know what MDN is) but want to see how you think, how you use your current knowledge and experience to tackle a problem you've never encounter because that's what programming is all about.

Your ability to communicate on what you are currently doing is also crucial to prove to your interviewer that you can take a few steps back and have a critical eye on a problem.

 

 

Feel free to comment if you liked that post and let me know what you think or tell me about an interview!

PS: I'll be adding more questions and refining that post. I'll be writing about more in-depth questions and the exercises you can be asked to solve during an interview.

PS2: I know I was supposed to post a video on how to replicate the Facebook landing page but I'm still waiting on Amazon to deliver my microphone. 😎