Engineering, Design, Marketing, and More

JavaScript interview questions

The most popular questions

Use our complementary questions and answers to filter and hire the best. Questions crowdsourced by our clients, answers by Punch. We provide these complementary questions to help our clients more quickly create tests for potential hires at their organizations.

Get a question answered
Punch offers four divisions of services: design, engineering, staffing, and demand

Interview questions for your next interview

Question
What is NaN? What is its type? How can you reliably test if a value is equal to NaN?
Answer
The NaN property represents a value that is “not a number”. This special value results from an operation that could not be performed either because one of the operands was non-numeric (e.g., "abc" / 4), or because the result of the operation is non-numeric (e.g., an attempt to divide by zero).

A semi-reliable way to test whether a number is equal to NaN is with the built-in function isNaN(), but even using isNaN() is an imperfect solution.

A better solution would either be to use value !== value, which would only produce true if the value is equal to NaN. Also, ES6 offers a new Number.isNaN() function, which is a different and more reliable than the old global isNaN() function.
Question
What is a “closure” in JavaScript?
Answer
A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically:
  • variable in its own scope
  • variables in the enclosing function’s scope
  • global variables

Find developers today

Hire a Punch engineer

Punch offers four divisions of services: design, engineering, staffing, and demand. Our four divisions form the core of our People Forward Approach.

Contact us
Find developers today
Question
What is the drawback of creating true private methods in JavaScript?
Answer
One of the drawbacks of creating true private methods in JavaScript is that they are very memory-inefficient, as a new copy of the method would be created for each instance. So, as a recommendation, don’t use private methods unless it’s necessary.
Question
How do you check if an object is an array or not?
Answer
The best way to find out whether or not an object is an instance of a particular class is to use the toString method from Object.prototype: if (Object.prototype.toString.call(arrayList) === '[object Array]') {
console.log('Array!');
}
Question
What is hoisting in JavaScript?
Answer
In JavaScript, a variable can be declared after it has been used. In other words, a variable can be used before it has been declared. Basically, the JavaScript interpreter looks ahead to find all variable declarations and then hoists them to the top of the function where they’re declared.

For example:x = 1;
console.log(x);
var x;
Also important to mention that JavaScript only hoists declarations and not initializations.

If a developer doesn't understand hoisting, programs may contain bugs (errors). To avoid bugs, always declare all variables at the beginning of every scope.
Question
What is the instanceof operator in JavaScript?
Answer
instanceof operator checks the current object and returns true if the object is of the specified type.

Example:var c = new Customer();
c instanceof Customer; // Returns true
c instanceof String; // Returns false as c is not a string
Question
Explain the concept of unobtrusive Javascript.
Answer
Unobtrusive JavaScript is basically a JavaScript methodology that seeks to overcome browser inconsistencies by separating page functionality from structure. The basic premise of unobtrusive JavaScript is that page functionality should be maintained even when JavaScript is unavailable on a user’s browser.
Question
What are the global variables in Javascript?
Answer
A variable declared outside a function, becomes global. A global variable has global scope - all scripts and functions on a web page can access it.var gretting = 'Hello, ';

function welcome(name) {
console.log(greeting + name)
}
Best practice is to minimize the use of global variables. This includes all data types, objects, and functions. Global variables and functions can be overwritten by other scripts.
Question
What types of functions does JavaScript support?
Answer
A function in JavaScript can be either named or anonymous.

Named function definition:function hello() {
console.log('Hello world');
}
hello();
An anonymous function can be defined in similar way as a normal function but it would not have any name.var anon = function() {
console.log('Anonymous function call');
};
anon();
One common use for anonymous functions is as arguments to other functions. Another common use is as a closure.
Question
What is an immediately-invoked function expression?
Answer
IIFE (Immediately invoked function expression) is a function which executes as soon as the script loads and goes away.(function() {
var name = 'Punch';
console.log('Hello ' + name + '!');
})();
One of its most common use case is to limit scope of a variable made via var. Variables created via var have a scope limited to a function so this construct (which is a function wrapper around certain code) will make sure that your variable scope doesn't leak out of that function.

In above example, name will not be available outside the immediately invoked function i.e. scope of count will not leak out of the function.

Find developers today

Hire a Punch engineer

Punch offers four divisions of services: design, engineering, staffing, and demand. Our four divisions form the core of our People Forward Approach.

Contact us
Find developers today
Question
What is a callback?
Answer
A callback is a plain JavaScript function passed to some method as an argument or option. Some callbacks are just events, called to give the user a chance to react when a certain state is triggered.
Question
What is a closure?
Answer
A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters. Note that the inner function cannot call the outer function’s arguments object, however, even though it can call the outer function’s parameters directly.
Question
What are the variable naming conventions in JavaScript?
Answer
While naming your variables in JavaScript keep following rules in mind.

You should not use any of the JavaScript reserved keyword as variable name. These keywords are mentioned in the next section. For example, break or boolean variable names are not valid.

JavaScript variable names should not start with a numeral (0-9). They must begin with a letter or the underscore character. For example, 123test is an invalid variable name but _123test is a valid one.

JavaScript variable names are case sensitive. For example, Name and name are two different variables.
Question
How would you handle exceptions in JavaScript?
Answer
The latest versions of JavaScript added exception handling capabilities. JavaScript implements the try...catch...finally construct as well as the throw operator to handle exceptions.

You can catch programmer-generated and runtime exceptions, but you cannot catch JavaScript syntax errors.
Question
Can I declare a variable as CONSTANT in JavaScript?
Answer
No, constants do not exist in JavaScript. But you can follow this naming convention:
  • Variable and function names written as camelCase
  • Global variables written in UPPERCASE (We don't, but it's quite common)
  • Constants (like PI) written in UPPERCASE
Question
Why would you include ‘use strict’ at the beginning of a JavaScript source file?
Answer
Using strict mode enforces stricter error handling when running your code. It essentially raises errors that would have otherwise failed silently. Using strict mode can help you avoid simple mistakes like creating accidental globals, undefined values, or duplicate property names. This is typically a good idea when writing JavaScript, as such errors can create a lot of frustrating side effects and be difficult to track down.
Question
Is there automatic type conversion in JavaScript?
Answer
Yes, JavaScript supports automatic type conversion. In the example below, JavaScript is expecting a string. When it receives a fixnum as an operand, it’s automatically converted to a string.5 + “ cats” = “5 cats”

Ask a question

Ask a question, and one of our engineers will answer it.

We keep our questions nice and simple to be useful for everyone, and we may not answer or publish every question.

Your number is stored privately and never shared.
By submitting a question, you agree to our terms.
Request sent. Thank you!
Send another one