Every big company uses JavaScript to build its web applications, so JavaScript developers are in high demand all around the globe. To get a job and build a career as a JavaScript developer, you need to ace the interview. You might be asked different questions in your interview depending upon the job you’re applying for and your experience level. This article contains questions for every experience level, from fresh graduates to more seasoned developers.
JavaScript interview questions
Here’s a list of the most common JavaScript interview questions and their answers:
Question 1: What is the difference between declaring variables using let, var, and const?
The var, let, and const all are used to declare variables; but the scope of the variables declared with let and const is restricted to the block (A set of curly braces defines a block of code), whereas the variables declared with var are scoped globally. Here’s an example that shows the scope of the variables:
var a = 1;
let b = 2;
console.log(a);
console.log(b);
}
console.log(a);
// b is undefined outside of the block as it was declared using let
console.log(b);
var a = 1;
const c = 3;
console.log(a);
console.log(c);
}
console.log(a);
// Similarly, c is also undefined outside of the block
console.log(c);
Another difference between var and let is that the variables created with var can be redeclared and redefined, whereas the variables created with the let keyword can only be redefined. Variables declared with the let keyword can also be redeclared in different blocks.
The variables declared with the const are block-scoped, and their values cannot be changed. They cannot be redefined or redeclared and have to be initialized during declaration.
Question 2: Differentiate between “=”, “==” and “===” operators?
The = is an assignment operator and is used to set the value of a variable. The == operator is used to compare the value of two variables, whereas the === operator compares the values and the types of two variables. The == operator will return true if we compare ‘1’(string) with 1(integer), whereas the === operator will return it as false:
'1' === 1
if ('1' == 1) {
console.log(true);
}
else {
console.log(false);
}
if ('1' === 1) {
console.log(true);
}
else {
console.log(false);
}
Question 3: Difference between “undefined” and “null”?
In JavaScript, a variable is assigned the undefined value by default if it has not been initialized, whereas the null value has to be manually assigned to a variable.
Question 4: How to assign properties to an object in JavaScript?
Two different methods are used to assign properties to objects in JavaScript:
- By using the dot “.” operator
- By using the square brackets syntax
user.age = 33;
user['id'] = 05;
Question 5: What is the difference between a statically and dynamically typed language?
Statically typed languages require you to define the variable’s data type while declaring it, whereas there is no such restriction in dynamically typed language. The dynamically typed languages perform type checks at runtime, whereas the statically typed programming languages perform tasks simultaneously.
A variable can be used to store any data type in a dynamically typed language, while in a static language, a variable can only store one data type.
JavaScript, Python, and Ruby are examples of dynamically typed languages, while C, C++, and Java are examples of statically typed languages.
Question 6: What is JavaScript hoisting?
JavaScript has a default behavior called hosting, which moves all the variable and function declarations to the top of the scope. A variable in JavaScript can be used before it is even declared.
Question 7: Difference between pop(), push() and shift(), unshift() methods
The pop() and push() methods are used to remove and add an element/item at the ending point of an array respectively. Whereas the shift() and unshift() methods are used to remove and add elements/elements from the start point of an array:
intArr.pop(); // returns the removed item
console.log(intArr);
intArr.push(67); // returns the new array length
console.log(intArr);
intArr.shift(); // returns the removed item
console.log(intArr);
intArr.unshift(23); // returns the new array length
console.log(intArr);
Question 8: What is the “this” keyword
In JavaScript, this keyword has a different meaning depending upon where it is being used. The this keyword used alone in JavaScript code refers to the global object, whereas when used with a method, it refers to the object which owns that particular method. It also refers to the global object when used in a function, but it is undefined in a function in strict mode. The this keyword used in an event points to the element that is receiving the event.
Question 9: What are classes in javascript?
Classes are not native and were only (relatively) recently added to JavaScript. They are just syntactic sugar built on top of prototypes. They are used as blueprints/templates for creating objects.
Question 10: What will the following code return:
The code is given above returns “string.” It can be divided into two different pieces:
- typeof 10
- The type of the returned value of typeof 10
Question 11: What does adding ‘use strict’ at the top of the JavaScript source file do?
The ‘use strict’ is added at the top of the JavaScript source file to run the code in strict mode. It converts JavaScript from a dynamically typed language to a lot more strict. It prevents you from using undeclared variables.
The ‘use strict’ makes the JavaScript code secure and encourages the developer not to use the bad syntax previously accepted in JavaScript.
Question 12: What are cookies?
Cookies are text files that store the information of the user at the time of browsing. They preserve the state of the user. Cookies are used to remember the user’s information each time the user sends a new request to the server.
JavaScript has a built-in property, “document.cookie,” which can be used to read, write and modify the cookie file.
Question 13: What is setTimeout in javascript?
setTimeout() is a native method of JavaScript that is used to call a function after a defined amount of time.
Question 14: How are javascript and node.js different?
JavaScript is a scripting language, whereas node.js is an environment and interpreter running JavaScript outside a browser.
Question 15: What is closure?
In JavaScript, closure is a function that is declared and defined inside another function. The function that is nested inside another function can access its own variables and the variables of its parent function, and the global variables.
Example:
functionparent()
{
var two = 2;
functionchild()
{
var three = 3;
console.log(one + two + three);
}
child();
}
parent();
Question 16. Can we store objects in an array of javaScript?
Yes, we can store anything in the array of JavaScript. Not even objects but arrays of objects within an array or functions.
Question 17. What is the datatype of an array?
The data type of an array in JavaScript is an object because it stores data and behaves totally like an object.
Using the typeof() operator over an array element will show the variable as an object.
console.log(typeof(arr));
Output
However, if you really want to know whether a variable is an array or object, you can use Array.isArray() method:
Question 18. Is there any associative array in JavaScript?
No, javascript does not support arrays with the named indices. For that purpose, objects are available in JavaScript.
Question 19: What does a map() function do in javascript?
The map() function is used when we need to iterate through the whole array and change the values of each element of an array.
For example, if we have a list of numbers and we want to add 5 to each number:
arr.map((n)=>{
return n+5;
});
Question 20: What will be the output of 30+50+“20”?
The output of the given expression will be 8020.
Since 30 and 50 are integers, they will be added as integers, and the answer will be 80. Later, “20” is a string, so 80 will be concatenated with “20,” and the final result will be “8020”.
Conclusion
Applying for a new job and going through an interview can feel like an overwhelming task and can cause great anxiety, but you need to trust yourself.
This article contains some of the most common interview questions asked by interviewers when applying for a “JavaScript developer” position.