Loading...
javascript interview questions and answers

JavaScript Interview Questions and Answers

by GangBoard Admin, March 8, 2019

In case you’re searching for JavaScript Interview Questions and answers for Experienced or Freshers, you are at the correct place. GangBoard offers Advanced JavaScript Interview Questions and answers that assist you in splitting your JavaScript interview and procure dream vocation as JavaScript Developer.

Q1) What are all the data types JavaScript supports?

Answer: JavaScript offers seven data types which are: Number, String, Boolean, null, undefined, Symbol (newly added in ECMAScipt 6) and Object.

Q2) How can you remove an element from an array? Or can you use delete to remove an element in an array or not?

Answer: First, we should not use delete to remove an element from an array. Because, delete removes only the value of the array element, not the entire element. So the index still have undefined. And the length does not change. delete is supposed to use on an Object to remove its property.

The best way to remove an element is using splice method. The syntax is:

arr.splice(startIndex, deleteCount, itemToInser);
 and 
[1, 2, 3].splice(1, 1);
 result in [1, 3].

Q3) What are all the types of function? Or what are all the different ways you can declare a function and how they differ?

Answer: There are mainly three ways, we could define a function:

Function declaration: This is the straight-forward method. It uses a function keyword followed by the name of the function, list of parameters and a pair of curly braces to define function body. This type of definition is hoisted, means this block will be lifted up to the top of the scope and can be invoked before the declaration.

function isEven(num)
 {
return num % 2 === 0;
}

Function expression or function assignment: In this way, we are declaring a function with or without a name and assign to a variable. This type of function is not hoisted means you cannot call this function before they are declaration happens.

var isEven = function(num)
 {
 return num % 2 === 0;
};

Anonymous function: Anonymous functions are unnamed functions and run immediately. The function is executed as soon as the declaration happens. And you won’t call it again because there are no name. They are widely used to wrap our code in a closure and not affecting the outer scope.

(function(num) 
{
 return num % 2 === 0;
})(2);

Or, the below is also the same.

(function(num) {
 return num % 2 === 0;
}(2));

Other than these three methods, JavaScript has Function constructor which has the syntax of var is Even = new Function(arg1, arg2, ‘return num % 2 === 0’);. This is not used much because it does not have access to the current scope and always created in the global scope.

And finally we have Arrow functions and Generator functions which are newly added in ECMAScript 6.

Q4) What is instanceof in JavaScript?

Answer: This operator tests whether an object has the prototype property of a given constructor in its prototype chain. For example,

function Car(name)
 {
this.name = name;
}
var mycar = new Car('Honda');
mycar instanceof Car; //true
mycar instanceof Car; returns true since the mycar is constructed from the Car.

Q5) How can you get the list of all properties in an Object?

Answer: The easy way is using Object.keys(). This will return an array of given object’s own enumerable properties.

If we want all properties, even not-enumerable properties also, we can use Object.getOwnPropertyNames().

Q6) What is Event capturing and Event bubbling?

Answer: It defines in which order the event handler should be executed. For example, consider an element which has click event attached, and its parent also has the same event handler attached to it. Now if the children has been clicked, then:

  • In event capturing mode, the parent element’s event handler is executed first and then the child element’s event handler is executed after that.
  • In event bubbling mode, the child element’s event handler is executed first and then the parent element’s event handler is executed after that.

Simply saying, the Event capturing is capturing the event handlers in top-down direction and Event bubbling is capturing the event handlers in bottom-top directions.

We can define which mode should be followed. In the EventListener syntax add the third parameter as true or false. The default value is false and the default execution phase is bubbling.

element.addEventListener(‘click’, doSomething, true) //Event handler is set to capturing phase.

element.addEventListener(‘click’, doSomething, false) //Event handler is set to bubbling phase.

Q7) What is the difference between window.onload and onDocumentReady?

Answer: onDocumentReady event is fired when DOM is loaded and it is safe to do any DOM manipulation after this event, so that the element is in the DOM. It will run prior to image and external contents are loaded.

window.onload will wait until the entire page is loaded including its CSS, images and fonts.

Q8) What is exception handling?

Answer: Exception handling is handling run time error in program. JavaScript provides try…catch statement to mark a block of code that can throw error in run time. If an exception is thrown at try block, the control will shifts to catch block. If no exception thrown, the catch block will be skipped. There is also one additional block, finally, at the end. It will run regardless of whether the try block failed or not.

Q9) What is shift() and push? How they are differing from each other?

Answer: ”The shift() removes the first element from an array and return it.’’ On the otherhand, unshift() adds the given element at the start of an array.” The push() adds the given element at the end of the array. “And, pop() removes the last element from an array.”

Q10) What is callback? How they work?

Answer: JavaScript allows a regular function to be passed as an argument to another function. This callback function will be invoked later inside the passed function. It is also called high-order functions. A common usecase for callback is event handlers.

$("#btn").click(clickEvent);

How they work: We are not passing the function with (). Because we are passing only the function definition. So that it will be invoked later. Since callback functions are closures. They have access to the containing function’s scope, and even from the global scope.

Q11) What is Browser Object Model or BOM?

Answer: This is used to access the Browser window. Using the global object window, we can access the browser. They provide many useful methods like:

  • console: accessing the console of the browser
  • alert, window.confirm and window.prompt: showing alerts or getting input from the user.
  • history, window.navigator: accessing history API and getting browser informations.

window.location: redirecting user or change urls

Q12) What is negative infinity?

Answer: The negative infinity is a number in JavaScript, which can derived by dividing a negative number by 0.

Negative infinity is less than zero when compared, and positive infinity is greater than zero.

Q13) What is Number and String Objects in JavaScript?

Answer: JavaScript have Number and String objects to work with the primitive types number and string. We don’t need to explicitly create and use Number and String objects. JavaScript uses them to convert the primitive types to object when we are accessing any properties or methods on the primitive types. For example,

var str = 'Javascript';
str.length //10

Here, we are declaring a variable str and assign a primitive string to that. When accessing the length property, the primitive types actually does not have the length property, but JavaScript compiler convert this primitive string to Object string, and the return the length property of it. To say more clearly, JavaScript returns new String(str).length. And this String object will be removed immediately from the memory once it returns the required value. So we won’t see any difference. Remember, declaring a string as var str = ‘Javascript’; is different from declaring as String object as

var str = new String('Javascript');. The latter will return object.
var str = new String('Javascript');
typeof str; //object

Q14) What is encodeURI and decodeURI in JavaScript?

Answer: The encodeURI is used to encode an URL by replacing special characters, like space, percent. For example,

encodeURI(‘www.example.com/this is a test url with %character’) will results in, www.example.com/this%20is%20a%20test%20url%20with%20%25character. Notice that the space and percentage symbol are replaced with %20 and %25. The decodeURI works in reverse way.

Q15) What is currying in JavaScript?

Answer: Currying means constructing a function that process with partial parameters and return a function which when invoked would accept remaining parameters and return the final output.

A simple example is,

var greeting = function(greeting) {
return function(name) {
  console.log(greeting + " "   + name + "!");
};
};
When invoking it,
var greetHello = greeting( "Hello" );
greetHello( "World" ); // Hello World!
greeting( "Javascript" ); //Hello Javascript!
We can also call the function immediately,
greeting( "World" )( "World" ); // Hello World!

The greeting function accepts a single argument and returns a function and when this returned function invoked sooner or later, it can accepts the remaining name parameter and return the final output.

Q16) What is NaN in Javascript?

Answer: NaN means Not-A-Number—the same as the value of Number.NaN. Several operations can lead to NaN result. Some of them are,

2 / 'a';
Math.sqrt(-2)
Math.log(-1)
0/0
parseFloat('foo')
The typeof NaN returns number. And comparing NaN with NaN yields false.
NaN === NaN // false

To check NaN, we could use isNaN(NaN) function, or in ECMAScript 6 Number.isNaN(NaN)

Q17) What is DOM

Answer: Document Object Model .

Q18) What is ES?

Answer: Ecma Script.

Q19) What is Javascript?

Answer: Scripting language, client side and server side both.

Q20) Is javascript case Sentiative?

Answer: yes.. variable and functions namespace.

Q21) Which is latest release in ECMAScript?

Answer: ES8.

Q22) Who is the founder of Javascript?

Answer: Netscape Navigator.

Q23) What is the difference between window and document?

Answer: window will be your current window of computer and document will be your current documenent in your page.

Q24) What are the difference between == and ===?

Answer: == checks only with data, but === checks data with datatype.

Q25) What is the use of typeof?

Answer: typeof will give the datatype of variable.

Q26) What is prototype in javascript?

Answer: Every object has prototype in javascript, so you can add the property to an object based on prototype. You can create the instant.

Q27) What is undefined?

Answer: In javascript, undefined means, variable is declared but doesn’t contain value.

Q28) What is not defined?

Answer: Varaible not yet declared.

Q29) What is closure?

Answer: variable have first its own scope, parent scope and global sope.

Q30) How do you empty an Array?

Answer: array.length = 0;

Q31) Check whether Object is an Array/Not?

Answer: isArray method.

Q32) What is instanceOf?

Answer: checks the current Object and Returns true, If the object of specified type.

Q33) How to find the length of associative Array?

Answer: Object.keys(counterarray).length

Q34) What is method?

Answer: It is a piece of code, associated with the object.

Q35) What is object?

Answer: It is collection of key value pair.

Q36) What is function?

Answer: It is a piece of code, but not associated with the object.

Q37) Syntax for Spread Attributes?

Answer: [ … {object} ].

Q38) What is the difference for let and const?

Answer: let will scope for block level and const values never change.

Q39) What is variable hoisting?

Answer: All the variable will go the top of the function and declare the variable.

Q40) What is IFFE?

Answer: Self executing Functions — no need to call the functions.

Q41) What is Default Parameters?

Answer: Sending the values as paramers in default context.

Q42) what is promise?

Answer: Used for asynchronous interations. it has 3 internal states, pending, fulfilled and rejected.

Q43) How to create an Object?

Answer: Object.Create();

Q44) What is function binding?

Answer: It is in advance javascript. Handles the events and callback functions in context of passing function in parameters.

Q45) What is constructor?

Answer: Constructor are defines with the fuctions. create a new object and pass value to this and returns new object.

Q46) Singleton Pattern in js?

Answer: It gives a way to code the logical unit, which can be accedes through single variable.

No Comments


    Leave a Reply

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

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

    *

    Online Training Quick Enquiry






    Get Free Online training

    Looking for Online Training