
Autor: 08.03.2024
JavaScript Developer - Most Common Interview Questions
We have prepared a set of the most popular questions for the JavaScript Developer position. The article is divided into several sections. In the first section, you will quickly check your knowledge with multiple-choice questions. In the next section, we will move on to general questions. At the end, we have also included tasks involving the implementation of specified functions. The questions from the article cover various aspects of the JavaScript language and show what you can expect in a job interview. Let's begin!
10 Common JavaScript Questions
Below you will find ten sample questions covering a wide range of topics in the JavaScript language. Check if you can answer them correctly.
Events
What is the effect of using the method "event.preventDefault()" in JavaScript?
A) Stops event propagation
B) Prevents the default event behavior
C) Causes event re-invocation
D) Disables event listening
Correct answer: B. This method is used to stop the default event handling and implement custom handling.
Dates
Look at the code below and answer what will be displayed on the console?
const date = new Date();
console.log(date.getMonth());
A) Month number (0-11)
B) Day of the week number (0-6)
C) Current year
D) Current hour
Correct answer: A. In JavaScript, months are numbered from 0 to 11.
Prototype
Which method allows adding a new property to an object's prototype?
A) Object.create()
B) Object.assign()
C) Object.defineProperty()
D) Object.prototype.hasOwnProperty()
Correct answer: C. Object-oriented programming in JS is based on prototypes. Using the defineProperty() method, we can add a new property to an object. Properties added in this way are by default protected from writing and listing.
Closures
The JavaScript code below contains a closure. What will the add() function return?
function add(a) {
return function(b) {
return a + b;
};
}
var addToFive = add(5);
console.log(addToFive(3));
A) 8
B) 15
C) 53
D) 35
Correct answer: A. Closures are a specific mechanism of the JS language. Thanks to closures, an inner function has access to variables of the outer function.
Filter
What is the use of the filter() method in JavaScript?
A) Creates a new array with the results of calling the provided function for each array element
B) Filters array elements based on a specified condition
C) Reduces the array to a single value based on the provided function
D) Executes a specified function for each array element
Correct answer: B. Filtering is one of the key techniques of functional programming. It allows us to filter out elements from an array that meet criteria specified by us.
Pure Functions
Which of the following statements about pure functions are true?
A) A pure function can change global variables
B) A pure function always returns a value
C) A pure function cannot use local variables
Correct answer: B. Pure functions are another important element of functional programming. A proper pure function should always return a value.
Inheritance
Which of the following fragments presents the correct way to call a method from the base class in the derived class?
A) super.methodName();
B) this.methodName();
C) base.methodName();
D) parent.methodName();
Correct answer: B. Using the super.methodName keyword, we can refer to a method from the base class in the derived class.
Objects
Select true statements about object methods in the JavaScript language:
A) Methods are functions belonging to objects
B) Objects cannot have methods
C) Methods are used only to create objects
Correct answer: A. A method is simply a function belonging to an object.
Arrays
How to remove the last element from an array in JavaScript?
A) array.shift()
B) array.deleteLast()
C) array.removeLast()
D) array.pop()
Correct answer: D. The pop() method removes the last element from the array.
Variables
How are variables "hoisted" in JavaScript?
A) Variables are not hoisted in JavaScript
B) Variable declarations are moved to the beginning of their scope
C) Variable declarations are moved to the end of their scope
Correct answer: B. Hoisting is a mechanism specific to JavaScript. It involves moving (hoisting) declarations automatically to the top of the scope. In practice, this allows calling a variable before it has been declared. However, you need to be careful with important differences between var and let declarations.
5 Open JavaScript Questions With Answers
How does prototype inheritance work in JavaScript?
Prototype inheritance in JavaScript means that each object inherits properties and methods from its prototype object. When we try to access a property or method of an object, JavaScript first checks the current object, and then its prototype object, traversing up the prototype chain until the Object object.
Explain the difference between var, let, and const in JavaScript.
var is hoisted and has function scope, let has block scope and is not hoisted, while const also has block scope and cannot be re-assigned after initialization.
What are Promises in JavaScript?
A Promise is an object representing an asynchronous operation that may succeed (resolve) or fail (reject).
How does hoisting work in the context of JavaScript?
Hoisting is a mechanism where variable and function declarations are moved (hoisted) to the top of their scope before the actual code execution.
What is Event Delegation?
Event Delegation is a technique where events are handled on a common parent element instead of every individual child element. This helps in more efficient event management, especially for dynamically generated elements.
Example JavaScript Assignments
During a job interview, you may also encounter tasks that require you to write code. Below, we have prepared three sample tasks.
Counting characters in a string
Your task: implement an algorithm to count spaces in a string of text. The spaceCounter() function will be responsible for counting spaces in the text string. We expect your function to produce the following results:
spaceCounter("Hello !") -> 1
spaceCounter(" ") -> 1
spaceCounter("") -> 0
spaceCounter("Hello JS !") -> 2
Your task in this case will be to implement the spaceCounter() function. Will you manage?
Joining characters in a string
Your task: implement an algorithm to join elements of a text string using an underscore (_).
Look at the tests for the connect() function. We expect your function implementation to return the following results:
connect("a") -> a
connect("ab") -> a_b
connect("abc") -> a_b_c
Calculating the median
Try to come up with a way to determine the median from a set of integers. Look at the tests for the median() function. We expect your function implementation to produce the following results:
median([1, 2, 3, 4, 5]) -> 3
median([2, 3, 1, 4, 5]) -> 3
median([1]) -> 1
median([3, 2]) -> 2.5
median([]) -> undefined
Your task in this case will be to implement the spaceCounter() function. Will you manage?
How to Prepare For a Job Interview
During an interview, you may encounter various tasks: tests, open-ended questions, as well as algorithm implementations in code. That's why in the article, we demonstrated the widest variety of such questions. Good luck!