top 10 important questions of javascript and typescript

Top 10 Important JavaScript Interview Questions and Answers

7 min read

Hello, If you are a front-end developer and want to crack a technical interview of a most popular programming language i.e JavaScript then you are in the right place.

In this post I am sharing 10 most important questions and solutions of JavaScript / TypeScript with you. As per my experience these questions are asked by interviewers many times. I will also try to share separate posts on some important topics for detailed discussion and explanations in future.

Before starting I am assuming that you have some basic knowledge of JavaScript and TypeScript.

Question 1)  What is Hoisting in JavaScript ?

Answer:  In JavaScript, hoisting is a default behavior to move all declarations (Variables & Functions) to the top of the current scope (to the top of the current script or the current function).
Key-point: Remember that interviewers can confuse you on “Variables and Functions”  So in hoisting both will be declared on the top of the current scope.

Question 2) What is the use of “use strict” ?

Answer: The “use strict” directive was introduced in ECMAscript version 5. It helps developers to write cleaner code, like preventing from using undeclared variables. It writes at the top of the js file.

Example:

"use strict";
pi = 3.14 ;       // This line will cause an error because pi is not declared

Question 3) Wh犀利士 y do we use $ in jQuery ?

Answer: As we all know that jQuery is a famous library of JavaScript. When we include it in our web page it creates its own object. So the $ (dollar) holds the references of that object by which we can use any jQuery method of the library.

Question 4) How to create a new Object in JavaScript ?

Answer: There are different ways to create an object in javascript.The simplest way by using bracket’s like

var obj = { };

Then you can use the Object constructor to create an object. Like

var myObj = new Object(); 

You can also use the create() method that was introduced in ES6.

Question 5) What are the differences between ES5 and ES6 ?

Answer: You can remember these simple points to give the answer of this question. 

  1. Constants – Constants are values that can be defined only once (per scope).
  2. Let – Block-scoped variable
  3. Arrow Functions..
  4. Default Function Parameters –  var x = (a, b = 1) => a * b
  5. String Templating- var myName= “Darshan”, var x = `Hello ${myName}`
  6. Formal Class Definition Syntax, Getters and Setters.

Question 6) What is call(), bind() and apply() methods in Javascript ?

Answer: As we all know that in JavaScript all the functions are Objects, So these 3 methods are used to control the invocation of the function. Method call() and apply() were introduced in ECMAscript version 3 (ES3) and bind() method introduced in ECMAscript version 5 (ES5).

Difference:

We can use call() and apply() methods to invoke function immediately
bind() method can be used when the function needs to be called later in certain events when it’s useful.

Question 7) What are the states of Promise in JavaScript ?
Answer:
Fulfilled: onFulfilled() will be called (example: resolve() method was called)
Rejected: onRejected() will be called (example: reject() method was called)
Pending: not yet fulfilled or rejected.

Question 8) What are generator functions in JavaScript ?

Answer: Generator functions are used to solve the problem of callback hell, It generates the series of results rather than a single value.
Yield method – It is called in a function to halt the execution of the function at the specific line.
Next method – This method is called from the main application to resume the execution of a function which has a yield method.

Question 9) How to create associative arrays in JavaScript ?

Answer: Associative arrays are not supported in JavaScript. Arrays in JavaScript uses numbered indexes. If you are using named indexed, So JavaScript will redefine the array to a standard object. 

Question 10) What are the differences between == and === in JS ? Answer:
== (Double equal) operator also known as equality operator/ comparison operator.
=== (Triple equal) operator also known as identity operator.
The main difference between them is equality operator checks only values but identity operator also check the type of the variable.

Thanks for reading this post. I hope you will get some basic idea of interview questions in JavaScript. Please upvote this post if you liked it and also share with those who are preparing for UI developer job roles.