JavaScript is on its way to becoming the world’s most popular programming language. As the demand grows for JS developers, you have to be ready to run for it. Here are the top 10 JavaScript questions to nail your next interview for your dream job.
What is the difference between var and let?
While this seems easy enough, you will not believe how many candidates I had to turn down just because they could not answer this question. The difference is in the level of scope. var
is function-scoped, but let
(and const
) are block-scoped. To understand the difference, check out this example:
What is the difference between == and ===?
If your answer is “==
compares by value and ===
also compares by type”, it is not correct. The way JS engine understands it, ==
allows type coercion and ===
disallows. Type coercion is the automatic type conversion by the interpreter. This is the source of most confusion in JS (like [] == ![]
being true). You can observe the difference in this snippet:
What does ‘this’ keyword mean?
You might be tempted to answer that this points to the instance of the class inside its body, but this is also not right. First off, classes in JS are syntactic sugar and do not introduce any new features. this
keyword is available in any function and points to the object that contains this function. It might be easier to understand it with an example:
What is a constructor function?
Constructor functions in JS are also not class-related functions and are tied closely to this
keyword. A constructor function is called with new
keyword and returns whatever the value of this
is. Note that in constructor functions this
does not point to the outer object, but instead used as a placeholder object:
Convert this callback-based call to a Promise-based one
It is more of an exercise that a question, but nevertheless it is something you have to know. Callbacks are simply functions that you intend to call at some later time. They are most often used when you have to wait for something (e.g. response from the API). But, callback-based code was too complicated and that is why Promises were introduced. I am not going to go into it here, but if you do now know what Promises are, check out this article. In the next example, you are asked to convert the getData
callback function into a Promise:
The Promise constructor function accepts a callback, which receives two functions: resolve
and reject
. Inside the callback, you perform your time-consuming tasks and call resolve or reject, based on the outcome.
NaN === NaN?
False. This is an endless source of debate and one of the most confusing parts about JS. In a nutshell, NaN
stands for Not a Number, and just because one value is not a number and another one is not a number does not imply they are equal. The downside is you cannot really check if a variable is NaN
using myVariable === NaN
. You can use the Number.isNaN
function or myVariable !== myVariable
to check for it.
0.1 + 0.2 === 0.3?
False. This trick does not apply only to JS: it is common among floating-point operations in any language. It has to do with the way the CPU processes floating-point numbers. The actual value of 0.1 + 0.2
will be something like 0.300000001
and to check for equality you would write Math.abs(0.3 - (0.2 + 0.1)) <= EPS
, where EPS is an arbitrary small value (0.00001, for example).
What are the primitive data types in JS?
A primitive data type in JS is data that is not an object and that has no methods. Here is the list of primitive data types in JS:
- Boolean
- Null
- Undefined
- Number
- BigInt
- String
- Symbol
What is “strict” mode?
In JS, you enable strict mode by putting "use strict";
at the beginning of the file. Strict mode enables more rigorous error-checking in your code and makes debugging easier. For example, this snippet will work in regular JS, but not strict:
What is the output of this code?
undefined
. This is happening because JS will inject a semicolon after return on line 2, and treat lines 3-5 as a scope instead of an object definition.
Thank you for reading, I wish you the best of luck acing your interviews! For more interesting content, check out my other articles:
- 7 really good reasons not to use TypeScript
- To infinity and beyond with JavaScript Proxy API
- Builder pattern in JavaScript
Very nice and crisp article. Interesting to read and understand with example. Thank you so much.