Online Interview Questions

Prepare interviews on Anything, AnyWhere

PHPSCOTS,Blogger, Developer Views: 151110
Basic JavaScript interview questions

Top Javascript interview questions for Freshers and experienced

1. Explain what is Javascript? List some data types supported by Javascript?


Javascript is an object-oriented computer programming language commonly used to create interactive effects within web browsers.It is first used by the Netscape browser, that provides access to the HTML document object model (DOM), provides access to the browser object model (BOM). Javascript syntax looks a lot like java, c or c++ syntax.

Below is the list of data types supported by Javascript:-

  • Undefined
  • Null
  • Boolean
  • String
  • Symbol
  • Number
  • Object

2. In Javascript are calculations with fractional numbers guaranteed to be precise?

NO, calculations with fractional numbers are not guaranteed to be precise in Javascript

3. List the comparison operators supported by Javascript?

Javascript supports below comparison operators

  • > Greater than
  • < Less than
  • <= Less than or equal to
  • >= Greater than or equal to
  • === Equal to
  • !== Not equal to

4. How do you declare variables in Javascript?

In Javascript variable are declared using the var keyword.A variable must begin with A letter$ or _.

eg. var myVar=”Online Interview Questions”;

PS: All variables in Javascript are Case sensitive.

Also, read Advanced JavaScript Interview Questions

5. What will happen if an infinite while loop is run in Javascript?

The program will crash the browser.

6. List HTML DOM mouse events?

HTML DOM mouse events

  • onclick
  • ondblclick
  • mousemove
  • mousedown
  • mouseover
  • mouseout
  • mouseup

7. How to get the last index of a string in Javascript?

string.length-1 is used to get the last index of a string in Javascript

Example Usage:-

var myString="JavascriptQuestions";

8. How to get the primitive value of a string in Javascript?

In Javascript valueOf() method is used to get the primitive value of a string.

Example Usage:

var myVar= "Hi!"

9. What are the primitive data types in JavaScript?

A primitive is a basic data type that’s not built out of other data types. It can only represent one single value. All primitives are built-in data types by necessity, (the compiler has to know about them,) but not all built-in data types are primitives.

In JavaScript there are 5 primitive data types are available they are undefinednullbooleanstring and number are available.Everything else in Javascript is an object.

10. What does the instanceof operator do?

In Javascript instanceof operator checks whether the object is an instance of a class or not:

Example Usage

Square.prototype = new Square();
console.log(sq instanceof Square); // true

11. What is Javascript BOM?

BOM stands for “Browser Object Modal” that allows Javascript to ‘talk’ to the browser, no standards, modern browsers implement similar BOMS – window, screen, location, history, navigator, timing, cookies.

12. What are different types of Popup boxes available in Javascript?

In Javascript there are 3 types of Popup Boxes are available, they are

  • Alert
  • Confirm
  • Prompt

Read 80+ Best Angular Js Interview Questions

13. How can you create an array in Javascript?

There are 3 different ways to create an array in Javascript. They are

  • By array literal

     var myArray=[value1,value2...valueN];
  • By creating instance of Array

    var myArray=new Array();
  • By using an Array constructor

    var myArray=new Array('value1','value2',...,'valueN');

14. What is the ‘Strict’ mode in JavaScript and how can it be enabled?

Strict mode is a way to introduce better error-checking into your code. When you use strict mode, you cannot, for example, use implicitly declared variables, or assign a value to a read-only property, or add a property to an object that is not extensible.

You can enable strict mode by adding “use strict”; at the beginning of a file, a program, or a function. This kind of declaration is known as a directive prologue. The scope of a strict mode declaration depends on its context. If it is declared in a global context (outside the scope of a function), all the code in the program is in strict mode. If it is declared in a function, all the code in the function is in strict mode.

15. How to calculate Fibonacci numbers in JavaScript?

Calculating Fibonacci series in JavaScript

Fibonacci numbers are a sequence of numbers where each value is the sum of the previous two, starting with 0 and 1. The first few values are 0, 1, 1, 2, 3, 5, 8, 13 ,…,

function fib(n) {
	var a=0, b=1;
	for (var i=0; i < n; i++) {
		var temp = a+b; 
		a = b;
		b = temp;
	return a;

16. What is the difference between the substr() and substring() functions in JavaScript?

Difference between the substr() and substring() functions in JavaScript.

The substr() function has the form substr(startIndex,length). It returns the substring from startIndex and returns ‘length’ number of characters.

var s = "hello";
( s.substr(1,4) == "ello" ) // true

The substring() function has the form substring(startIndex,endIndex). It returns the substring from startIndex up to endIndex – 1.

var s = "hello";
( s.substring(1,4) == "ell" ) // true

17. What are different types of Inheritence? Which Inheritance is followed in Javascript.

There are two types of Inherientence in OOPS Classic and Prototypical Inheritance. Javascript follows Prototypical Inheritance.

18. What is output of undefined * 2 in Javascript?

nan is output of undefined * 2.

19. How to add/remove properties to object dynamically in Javascript?

You can add a property to an object using object.property_name =value , delete object.property_name is used to delete a property.


    let user = new Object();
    // adding a property'Anil';
    user.age  =25;
    delete user.age;

20. How to convert javascript date to ISO standard?

toISOString() menthod is used to convert javascript date to ISO standard.It converts javascript Date object into a string, using the ISO standard.


var date = new Date();
var n = date.toISOString();
// YYYY-MM-DDTHH:mm:ss.sssZ

21. How to get inner Html of an element in JavaScript?

innerHTML property of HTML DOM is used to get inner Html of an element in JavaScript.

Example Usage:

This is inner Element
		<script type="text/javascript">
			 var  inner= document.getElementById("inner").innerHTML ;
			 console.log(inner); // This is inner Element
			 document.getElementById("inner").innerHTML = "Html changed!";
			 var  inner= document.getElementById("inner").innerHTML ;
			 console.log(inner); // Html changed!

22. How to clone an object in Javascript?

Object.assign() method is used for cloning an object in Javascript.Here is sample usage

	var x = {myProp: "value"};
	var y = Object.assign({}, x); 

Leave a Reply

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