50 Most Asked Javascript Interview Questions & Answers [2020]

Table of Contents

Javascript Interview Question and Answers

In this article, we have compiled the most frequently asked JavaScript Interview Questions. These questions will give you an acquaintance with the type of questions that an interviewer might ask you during your interview.
JavaScript is a scripting language that is extremely useful in web development. Brendan Eich developed JavaScript back in 1995 and it is used by giants like Facebook and Google. It is quite impossible to fathom the versatility of JavaScript. Interviewers are always trying to gauge the knowledge of a prospective employee before handing them the appointment letter.

So, it’s essential to brush up on your JavaScript knowledge. Here are some of the Javascript interview questions that an interviewer can ask you if you are going for an interview.

The javascript interview questions and answers have been divided into three categories:

Javascript interview questions and answers – Beginner Level

Q1. How are JavaScript and Java different from each other?

Javascript is Object-oriented programming or OOP scripting language. Java is Object-oriented programming or OOP programming language

JavaScript can be run only on a browser. Java helps in creating applications that can be run on a browser or a virtual machine.

JS is presented in textual form. Java requires compilation

Q2. What is JavaScript?

JavaScript is a lightweight and interpreted programming language with object-oriented capacity. It helps you to develop interactivity into static HTML pages.. 

Q3. What data types do JavaScript support?

There are a few data types that JavaScript supports, which are:

  • Boolean
  • Null
  • String
  • Undefined
  • Symbol
  • Object
  • Number
    javascript-data types

Q4. What are the different features of JavaScript?

  • Javascript is a lightweight and interpreted programming language.
  • It has been designed to create applications which are network-centric.
  • It is complementary to Java and integrated with it.
  • JavaScript is a cross-platform and open scripting language.

Q5. Is JavaScript case-sensitive?

Yes , JavaScript is case-sensitive.

Q6. What advantages does JavaScript have?

Some advantages are:

  • Lower interaction with the server: With the help of JavaScript, you will be able to validate any user-based input before you send off the page to the server. 
  • Instant feedback to visitors: Visitors can immediately find out if they forgot to enter anything even before the page reloads. 
  • Increase in Interactivity: With JavaScript, you can create different interfaces which can react once the user hovers the mouse over them or uses the keyboard to activate them.

Q7: How can one create an object using JavaScript?

Since JavaScript is an Object-oriented programming scripting language, it supports the concept of Object. By using Object literal, you can create an object. 

Q8. How can one use JavaScript to create an Array?

If you want to define arrays in JavaScript, you can do so by using an array literal. Example:

var x = [];

var y = [1, 2, 3, 4, 5];

Q9. What is the name function in JavaScript and how can you define it?

A named function in JavaScript declares a name once it gets defined. Example:

function named(){

// write code here

}

Q10. Can one assign an anonymous function to a variable and then pass it as an argument to another function?

With Javascript, it is possible to assign an anonymous function to a variable and pass it as an argument in another function.

Q11. In JavaScript, what is argument object and how can you get argument types passed to a function?

Variable arguments in JavaScript represent arguments that are passed to a function. You can use typeof operator to get the type of the arguments. 

Q12. In JavaScript, what are the scopes of a variable?

Scope of a variable means the region of your program within which it is defined. There are two scopes:

  • Global Variables: A global variable has a global scope meaning that it will be visible everywhere within your code.
  • Local Variables: Local variables will only be visible within the function in which it has been defined.

Q13. What does the ‘This’ operator in JavaScript do?

The ‘This’ keyword used in JavaScript talks about the object which it belongs to. It has many different values and it depends on where it is being used. 

Q14. What is referred to as ‘Callback’?

A callback is a JavaScript function passed to some method as an option or an argument. The Callback is a function that needs to be executed after another function has finished executing.

Q15. Define Closure?

Closure is developed when a specific variable is defined outside the current scope and it is accessed from within with some inner scope. 

Q16. What are some of the built-in methods in JavaScript and what are the values returned by them?

Some of the built-in methods and the values returned by them are:

Concat() helps to join two or more than two strings

CharAt() helps to return the character at the specific index

forEach() helps to call a function for each element present in the array

length() helps to return the length of the string

indexOf() helps in returning the index within the calling String object of the first occurrence of the specific value

push() helps to add one or more than one element to the end of an array and then return the new length of that array

pop() helps to remove the last element from an array and return that element

reverse() helps to reverse the order of elements of an array

Q17. What are a few conventions of naming variables in JavaScript?

A few rules are:

  • One should not use any JavaScript reserved keyword as the variable name. 
  • Variable names in JavaScript cannot start with a numerical that is within 0-9. 
  • Variable names in JavaScript are case sensitive. 

Q18.  What is the work of the TypeOf Operator?

The typeof operator can be used to get the datatype of its operand. The specified operand can be a data structure or a literal such as a function, object or a variable.

Q19. How can you create a cookie with the help of JavaScript?

You can create a cookie in JavaScript simply by assigning a string value to the document.cookie object. 

The syntax:

document.cookie = “key1 = value1; key2 = value2; expires = date”;

Q20. How can you use JavaScript to read a cookie?

You can read a cookie as simply as creating a cookie in JavaScript as it is actually the value of the document.cookie object. If you want to access that specific cookie, you can use this string any time. 

  • By using the document.cookie string, you can keep a list of name – value pairs which are separated by semicolons, where the name is actually the name of a cookie and the value is the string value.
  • You can also make use of strings’ split() function to break the string into values and keys.

Q21. How can you use JavaScript to delete a cookie?

If you wish to delete a cookie so that any subsequent attempts made to read the cookie will not return anything, then, you have to set an expiration date of the cookie to a past time. 

Let’s move forward to more intermediate level javascript interview questions which will really test your JavaScript knowledge.

Javascript interview questions and answers – Intermediate Level

Q22. How are Attributes different from Property?

Attribute provides more details of an element like type, id, value, etc.

Property is the specific value assigned to the property. For example, value = ‘Name’, type = “text”, etc.

Q23. Give a list of the various ways using which an HTML element can be accessed within a JavaScript code?

A few ways are:

  • getElementById(‘idname’): Using this method, you can get an element by the ID name of the element.
  • getElementsByClass(‘classname’): Using this method, you can get all elements which have a given classname.
  • getElementsByTagName(‘tagname’): Using this method, you can get all elements which have a given tag name.
  • querySelector(): The querySelector() function takes the css style selector and returns the first selected element.

Q24. What are the different ways a JavaScript code can be involved within an HTML file?

The three different ways:

  • Inline
  • External
  • Internal

The JavaScript function known as the inline function is assigned to a variable that is created at runtime. On the other hand, if you require a JavaScript for function, you can integrate the script on the page on which you are working or you can place it as a separate file which can be called, when needed. This essentially, becomes the difference between external and internal script.

Q25. What are the different ways in which you can define a JavaScript variable?

There are 3 ways:

  • Var – The JavaScript variable statement helps to declare a variable. Alternatively, it can also be used to initialize the value of that specific variable. 
  • Const: The const functions are used so as to not allow the modification of the object on which it is called. So, when a function is said to be const, the function can be called on any object type.
  • Let: Let is used as a signal that shows that a variable can be reassigned, like a counter in a loop or that value swap within an algorithm. 

Q26. What is typed language?

In Typed Language, values are associated with values alone. They are not associated with variables. There are two types of Typed Language:

Get our free ebook!
Computer Vision with Successful Applications in Healthcare, Retail and More
Download Now

Dynamically: For Dynamically Typed language, the variable can hold several types. 

Statically: In Statically Typed languages, the variable is capable of holding only one type. 

Q27. How is Local Storage different from Session Storage?

Local Storage – In local storage, the data will not be returned to the server at every HTTP request (images, HTML, CSS, JavaScript, etc). It helps to reduce the traffic between server and client.  

Session Storage – Session Storage is quite similar to the local storage. However, unlike data storage in local systems which has an expiry time, data stored using session storage gets cleared once the page session ends. 

Q28. What are the differences between operator ‘==’ and ‘===’?

The difference between operator “==” and operator ‘===’ is that the former compares the variable by making a type correction. So, if you were to compare a number with a string with numeric literal, such comparison can be made using ‘==’ but not ‘===’. ‘===’ checks the value and the type of two variables. 

Q29. How are null and undefined differences?

Undefined refers to a variable which has been declared but the value assignment of the data has not been conducted. However, null is itself an assignment value. 

Q30.  How are undeclared and undefined different?

Undeclared variables are those kinds of variables which do not exist in a program and hence, are not declared. If the program attempts to read it, there will be a runtime error. Undefined variables are variables which are declared but there has been no value given to them. 

Q31. Name a few JavaScript Frameworks?

JavaScript frameworks mean an application framework which has been written in JavaScript. A few common ones are:

  • React
  • Angular
  • Vue

Q32. How are the JavaScript window and JavaScript document different from one another?

Window is a global object and it holds functions, variables, location and history.

Document is a part of the window and is deemed as a property of the Javascript window.

Q33. How are innerText and innerHTML different?

innerText – innerText does not process an HTML tag if it is found within a string.

innerHTML – innerHTML processes an HTML tag if it is found within a string.

Q34. In JavaScript, what is known as event bubbling?

Event bubbling refers to a specific way of event propagation in HTML DOM API. This happens when an event occurs within an element inside of another element and when both elements have a handle registered for the said event. When it comes to bubbling, the event which is first captured by the innermost element is eventually relayed to the outer elements.

Once the execution begins from one event, it goes out to the parent element. After that, the execution passes on to the parent element and this continues till the body element.

Q35. In JavaScript, what is NaN?

NaN stands for Not a Number. NaN is always used to compare unequal to any number, which includes NaN itself. Hence, it is generally used to bring out an error condition for any function that can return a valid number. When a string, for example, is converted into a number and if it cannot be done, then the NaN shows up.

Q36. How are JavaScript primitive/object types passed through functions?

Primitive data types are passed By Value while Objects are passed By Reference.

  • By Value means that it develops a copy of the genuine or original sd if it is a twin.
  • By Reference means that it creates an ALIAS of the genuine or original. You can compare it with common nicknames.

Q37. How to convert string of any base to an integer using JavaScript?

By using the parseInt() function, you can transform numbers among different bases. 

Q38. In JavaScript, what will be the result of the problem: 2+5+“3”?

  1. Since 2 and 5 are integers, normal addition will be executed. 3 however is a string and hence, there will be concatenation. “” represents a string.

Q39. What are imports and exports?

Imports and exports are useful to write modular JavaScript code. It can be split up further into multiple files.

You are already mastering some of the tough javascript interview questions. Now, it’s time to dive deep and go for some advanced javascript interview questions and answers.

Javascript interview questions and answers – Advanced Level

Q40. What is known as the Strict mode and how can you enable it?

The Strict mode helps you to check errors in your code. When you are using strict mode, you will not be able to use implicitly declared variables nor can you assign any value to read-only property.  

If you want to enable the strict mode, all you have to do is add “use strict” at the start of a program, function or file.

Q41. In JavaScript, what is the use of the prompt box?

The prompt box in JavaScript helps the user to input with the help of a text box. The prompt() method helps to display the dialog box which prompts the visitor to provide an input. 

Q42. What outcome will come out of this code:

var Y = 1;

if (function F(){})

{

y += Typeof F;</span>

}

console.log(y);

1undefined will be the output. This is because the if condition statement will evaluate using ‘eval’ and hence, eval(function f(){}) will return function f(){} (true). So, inside this if statement, one would execute the typeof f which will return undefined since the if statement code is executed at run time. Thus, the statement present inside the if condition is going to be evaluated during the run time.

Q43. State the difference between Apply and Call?

The call() method helps to call a function which has a given ‘this’ value and the arguments which are individually provided. The syntax is:

fun.call(thisArg[, arg1[, arg2[, …]]])

The apply() method is used to call a function which has a given ‘this’ value but the arguments are presented as an array. The syntax is:

fun.apply(thisArg, [argsArray])

Q44.In JavaScript, how can you empty an Array?

There are a few methods to empty an array. They are:

Method 1:

arrayList = []

If you do not have any references to the original array arrayList, this method is recommended. However, if you have previously referenced this array from a different variable, then the original reference array will be kept unchanged.

Method 2:

arrayList.length = 0;

By using this code, you are setting the array length to 0 thus emptying it of all updates of reference variables, going back to the original array. 

Method 3:

arrayList.splice(0, arrayList.length);

This method can also be used to empty all the array including updates of all the references back to the original array.

Method 4:

while(arrayList.length)

{

arrayList.pop();

}

This is also a great way to empty arrays but it is not a recommended method.

Q45. What output will this code present:

var Output = (function(x)

{

Delete X;

return X;

}

)(0);

console.log(output);

Delete operator helps to delete the properties from an object. In this code, x is not used as an object but a local variable. Delete operators do not have any effect on local variables.

Q46. What output will this code present:

var X = { Foo : 1};

var Output = (function()

{

delete X.foo;

return X.foo;

}

)();

console.log(output);

Undefined output. Delete operator helps to delete the properties from an object. In this code, x is an object with the property used foo. This is a self-invoking function and hence, one would delete the property foo from x object. Hence, the result will be undefined.

Q47. What output will this code present:

var Employee =

{

company: ‘xyz’

}

var Emp1 = Object.create(employee);

delete Emp1.company Console.log(emp1.company);

Outcome = xyz. In this code, emp1 object uses company as its prototype property. Delete operator does not delete prototype property. The object emp1 does not have company as its property. It is possible to delete company property from the Employee object directly by using delete Employee.company.  

Q48. What output will this code present:

//nfe (named function expression)

var Foo = Function Bar()

{

return 7;

};

typeof Bar();

Output = Reference Error. The function definition can only have one reference variable as the function name.

Q49. Why is it common to wrap the content of JavaScript source file in a function book?

Many JavaScript libraries use this technique. It helps to develop a closure around the contents of the file which helps to create a private namespace and hence, avoid any name clash with different JavaScript modules and libraries. 

Q50. In JavaScript, what are escape characters?

Escape characters help you to write special characters without having to break the application. 

Conclusion

These are some popular javascript interview questions provided in varying difficulty to help you ace the interviews. Brushing up your basic and advanced knowledge of JavaScript using these javascript interview questions and answers are a great way to land a job as a developer in any top tech firm.

Some people prefer the artistic side of web development and some others prefer the technical one. But there’s a third group of people who know both. The first group is of front-end developers, the second one is of back-end developers, and the last one is of full-stack developers.

The demand for full-stack developers is on the rise. And companies are in desperate need of talented professionals who can work with both HTML and PHP. 

If you’re interested to learn more about full stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

What is JavaScript?

JavaScript is a lightweight and interpreted programming language with object-oriented capacity. It helps you to develop interactivity into static HTML pages.

Is JavaScript case-sensitive?

Yes , JavaScript is case-sensitive.

What are the scopes of a variable in JavaScript ?

Global Variables: A global variable has a global scope meaning that it will be visible everywhere within your code.
Local Variables: Local variables will only be visible within the function in which it has been defined.

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK DEVELOPMENT
Learn More

Leave a comment

Your email address will not be published. Required fields are marked *

×
Become A Full Stack Developer
Download syllabus & learn from a cutting-edge software development course designed by upGrad & IIIT-B.
Download syllabus
By clicking Download syllabus, I authorize upGrad and its representatives to contact me
via SMS / Email / Phone / WhatsApp / any other modes.
I agree to upGrad terms and conditions and our privacy policy.
Download EBook
Download EBook
By clicking Download EBook, you agree to our terms and conditions and our privacy policy.
Get our free ebook!
Computer Vision with Successful Applications in Healthcare, Retail and More
Download Now