Acing a JavaScript interview is important for both freshers and experienced developers. Preparing JavaScript interview questions can ultimately increase your chances of cracking interviews.
Whether you are a fresher looking for your first job or an experienced developer, it is good to understand the types of interview questions. And help you to answer those questions effectively.
In this article, you will get to know all types of common and practical JavaScript interview questions.
Table of Contents
JavaScript Interview Questions for Freshers
For freshers, having a solid grasp of fundamental JavaScript concepts is essential. Entry-level positions typically require candidates to demonstrate their understanding of basic syntax, data types, and essential functions. A strong foundation shows that you are prepared to tackle more complex problems as you gain experience.
Common JavaScript Interview Questions for Freshers
-
What is hoisting in JavaScript?
- Hoisting in JavaScript allows variables to be used before declaration. Let's see the following code first:
// Accessing `counter` before declaration console.log(counter);- The above code will result in
Uncaught ReferenceError: counter is not defined. It means that thecountervariable is not declared yet. So let's declare thecountervariable.
// Accessing `counter` before declaration console.log(counter); // Output: undefined // Declaring `counter` after its use var counter;- The above code will not produce the error:
Uncaught ReferenceError: counter is not defined. Because JavaScript hoisted thecountervariable. var,letandconstall are hoisted in javacsript. However,letandconstbehave differently.
-
Difference between
varandletkeyword in JavaScript.- You can use both
varandletto declare a variable. varis used to declare function scoped variable. Whileletis used to declare block-scoped variable.
- You can use both
-
Is JavaScript a statically typed or a dynamically (loosely) typed language?
- JavaScript is a dynamically typed language. JavaScript checks the type of variable during run-time. For example:
// data type of variable `name` is string var name = "Arun"; // now data type of variable `name` is number name = 34- In the above code, if you assign the value
Arunto thenamevariable. Then JavaScript automatically assigns the data typestringto thenamevariable. - And when you assign the value
34to thenamevariable. Then JavaScript automatically assigns the data typenumbertonamevariable.
-
What are the different data types in JavaScript?
- Primitive Types: String, Number, BigInt, Boolean, Undefined, Null, Symbol
- Non-primitive Types: Object, Array, function (it is also an object)
-
What is NaN property in JavaScript?
- NaN is a type of
numberand stands for "Not a Number". - You can check whether a variable is a number or not using the function
isNaN().
- NaN is a type of
-
Explain pass-by value.
- Let's consider the following example.
// initializing value of a let a = 3; // assigning variable a to variable b (pass by value) let b = a; b = 4; console.log(a) // Output: 3 console.log(b) // Output: 4- In the above code, you are assigning variable
ato variableb. - But when you change the value of variable
b. It doesn't change the value ofa. Because JavaScript copies the value of variableato the variableb. That is called the pass-by value.
-
Explain pass-by reference.
- Let's consider the following example:
const mobileAccessories = { charger: "Type-C", earPhone: "Bluetooth Neckband", cover: "Transparent Silicon" }; console.log(mobileAccessories); // Output: {charger: 'Type-C', earPhone: 'Bluetooth Neckband', cover: 'Transparent Silicon'} // Assigning mobileAccessories to mobileAccessoriesCopy (pass by reference) const mobileAccessoriesCopy = mobileAccessories; mobileAccessoriesCopy.charger = "Micro USB"; mobileAccessoriesCopy.cover = "Transparent Plastic"; console.log(mobileAccessories) // Output: {charger: 'Micro USB', earPhone: 'Bluetooth Neckband', cover: 'Transparent Plastic'}- In the above code, you are assigning the
mobileAccessoriesobject to themobileAccessoriesCopyobject. And when we change the properties of themobileAccessoriesCopyobject, JavaScript will also change the properties ofmobileAccessoriesobject. That is called passing by reference.
-
What is the use of a constructor function in JavaScript?
- You can use the constructor function to create objects in JavaScript. For example:
function Person(name, age, gender) { this.name = name; this.age = age; this.gender = gender; }- Now, you can create multiple object that has the properties
name,age, andgenderusingnewkeywords andPersonconstructors. For example:
function Person(name, age, gender) { this.name = name; this.age = age; this.gender = gender; } var detailsOfArun = new Person("Arun", 24, "Male") console.log(detailsOfArun); var detailsOfAruni = new Person("Aruni", 22, "Female") console.log(detailsOfAruni); -
Explain Implicit Type Coercion in JavaScript.
JavaScript automatically converts the data types of value. that is called implicit type coercion.
- String Coercion: When you use
+operator. JavaScript will convert the value of typenumberto the value of typestring. For example:
let a = 5; let b = "8"; console.log(a + b); // Output: 58- Equality Coercion: When you use
==operator, it will converts both variable to the same type before comparing them. For example:
let age = 18; let rollNo = "18"; // Convert both variable to sam data type before comparing console.log(age == rollNo); // Return true - String Coercion: When you use
-
What is an Immediately Invoked Function in JavaScript?
A function that is invoked immediately as soon as you define, is called IIFE (Immediately Invoked Function Expression). For example:
(function() { console.log ("I am an IIFE"); })(); -
Explain Higher Order Functions in JavaScript.
- A function that takes a function as an argument is called higher-order function.
- A function that returns a function is also called higher-order function.
-
Explain the
thiskeyword.- When you use
thisonly as shown below,thisrefers to the global object. For example:
let a = this; console.log(a); // Output: window- When you use
thisinside a function as shown below,thisrefers to theglobalobject. For example:
function sayHello() { console.log("Hi there!"); console.log(this); } sayHello(); - When you use
-
Explain call(), apply() and, bind() methods.
- Consider an example below:
const person = { name: "Arun", sayHello: function() { console.log(`Hello ${this.name}`); } }; const anotherPerson = {name:"Pratap"}; // borrow function sayHello from person object // And apply it to the object `anotherPerson` using call method person.sayHello.call(anotherPerson); // Output: Hello Pratap // borrow function sayHello from person object // and apply it to the object `anotherPerson` using apply method person.sayHello.apply(anotherPerson); // Output: Hello Pratap // copy sayHEllo function from person object // And use it for anotherPerson person.sayHello.bind(anotherPerson); // Output: ƒ () {console.log(`Hello ${this.name}`);}- In the above code, you can see both the
call()andapply()methods are usingsayHellofunction from thepersonobject. And ApplysayHellofunction to theanotherPersonobject. Just like you borrow a pen from your friends. - On the other hand, the
bind()method copy thesayHellofunction frompersonobject. And you can use it later. Just like you copy the homework from your friend's notebook to your notebook.
-
What is currying in JavaScript?
- Consider that you have a friend who loves ladoos. You know they'll always share two ladoos with you, but they like to give them one at a time. Here's how it works in code:
function addTwoNumbers(x1) { return function (x2) { return x1 + x2; } } const add10 = addTwoNumbers(10); // Stores the first laddoo (10) add10(15); // Now you have the second laddoo (15), and LADOO TIME! (returns 25)- In the above example,
addTwoNumbers(10)acts like your friend giving you the first laddoo (the value 10). It returns a new function that "holds" this value and waits for the second one. - Then, when you call
add10(15), it's like receiving the second laddoo (the value 15) and finally enjoying both together (resulting in 25).
-
Explain Scope and Scope Chain in JavaScript.
There are generally two types of scope:
- Global Scope: The variable and functions that are accessible from anywhere in your code, have global scope
- Local Scope: The variable and functions that exist inside a functions, have local scope. You can access the inside the function only.
- Now, consider an example to understand the scope chain in JavaScript:
function outerFunction() { const outerVar = "outer value"; function innerFunction() { const innerVar = "inner value"; // You can access outerVar from outer function's scope console.log(outerVar); } innerFunction(); } outerFunction();- In the above example, when you print the
outerVarvariable insideinnerFunction, then JavaScript first checks theouterVarvariable inside local scope (i.e.innerFunction). - Then, JavaScript moves to the function
outerFunctionto find the variableouterVar. When JavaScript finds the variable, then JavaScript prints the variableouterVarto the console. And that's how a scope chain works.
-
Explain Closures in JavaScript.
- Consider the following example:
function outerFunction() { const outerVar = "outer value"; function innerFunction() { const innerVar = "inner value"; // You can access outerVar from outer function's scope console.log(outerVar); } return innerFunction; } // call outer function and store it to variable `returnInnerFunction` const returnInnerFunction = outerFunction(); returnInnerFunction();- In the above example, you call
outerFunctionand store it to variablereturnInnerFunction. - And you know that function
outerFunctionis already executed. Still when we run the functionreturnInnerFunction. JavaScript remembers the scope of the variableouterVar(i.e.returnInnerFunctioncan access the variableouterVar).
-
What are object prototypes?
- Object prototypes are like human DNA, as they contain all the necessary properties and functions for objects.
- When you create an array in JavaScript, the array inherits all its properties and methods from its array prototypes, Just like how children inherit their characteristics from parental DNA. For example:
var arr = ["1","2"]; console.log(arr.length);- In the above example, you didn't define the property
lengthexplicitly. But JavaScript didn't show any error. Because JavaScript has built-in prototypes with these properties. Similar to how humans inherit characteristics from DNA.
-
What are callbacks?
- When you pass one function as an argument to another function. Then, it is called callbacks.
function greet(name, callback) { console.log("Printing..."); setTimeout(()=>callback(name),1000); } function printMyNameAlexa (name) { console.log(`Hey ${name}`); } // calls `greet` function And greet("Arun", printMyNameAlexa);- In the above example, you pass the function
printMyNameAlexaas an argument to the functiongreet. In this scenario, the functionprintMyNameAlexais considered a callback function because the functiongreetcalls (call-back) the functionprintMyNameAlexa.
-
What is recursion in a programming language?
- Recursion is process in which a function call itself again and again.
function decreaseNumber(n) { if (n >= 0) { console.log(n); // Recursion: call decreaseNumber // inside decreaseNumber repeatedly decreaseNumber(n - 1); } } // print the numbers line by line // 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 decreaseNumber(10);- In the above example, the
decreaseNumberfunction takes a number10and recursively calls itself until10becomes zero.
-
What is DOM?
- The Document Object Model (DOM) is a tree-like structure that helps the browser in rendering HTML pages. The DOM stands for Document Object Model. For example:
<!DOCTYPE html> <html> <head> <title>DOM Tree</title> </head> <body> <h1>I am a heading</h1> <p>I am a paragraph.</p> <button>Follow Me</button> </body> </html>- In the above example,
htmltag helps the browser to determine the document type as HTML and then render all the HTML tags accordingly by creating a DOM tree.
html ______|______ | | head body | ______|______ title | | | h1 p button -
Which method is used to retrieve a character from a certain index?
- You can use
charAtmethod in JavaScript to get the character at given index. For example:
var stringChars = "Quedemy"; // get character for index 3 console.log(stringChars.charAt(3)); // Output: d- In the above example, you use
charAtmethod that gets the character at index3.
- You can use
Adding more questions tomorrow...
