JavaScript Interview Questions

Question 1:

Are JavaScript variables and methods case sensitive?

Yes JavaScript variables and methods are case sensitive.

For example:-

var imgName; 
var imgname;

imgName and imgname are two different variable in JavaScript. Similarly,

function mymethod(){

function myMethod(){


Similarly mymethod() and myMethod() are two different methods in JavaScript. In JavaScript the case sensitivity does not just apply to variable names and methods but also to JavaScript keywords, event handlers, and object properties.

Question 2:

What are the different ways of making comments in JavaScript?

JavaScript supports both single line comment as well as multiline comment as shown in below examples:

Single Line Comment

// This variable holds value of data variable
var  data ="Educational Website";

Any text/code after // is considered as comment in JavaScript.

Multi-Line Comment

/* This code is used to store value of
    various elements of HTML DOM 
var domElement1 = document.getElementByID("div1").value;

Any text/code between /* and */ is considered comment in JavaScript.

Question 3:

What are the various datatypes supported by JavaScript?

In JavaScript, there are three primary data types, two composite data types, and two special data types.

Primary Data Types

The primary (primitive) data types are:

  • String
  • Number
  • Boolean

Composite Data Types

The composite (reference) data types are:

  • Object
  • Array

Special Data Types

The special data types are:

  • Null
  • Undefined
Question 4:

What is the difference between == and === ?

JavaScript provides both strict(===) and type-converting equality(==).  A strict comparison (e.g., ===) is only true if the operands are of the same type whereas == converts the operands to the same type before making the comparison.

For example:

10 == 8  false
10 == 10 true
10 == "10"  true
10 === 10  true
10 === "10"  false
Question 5:

What will be evaluated value of calculatedValue for following expression

var calculatedValue = "1"+2+3;

In the above expression, the calculation is started from left to right and "1" is considered as string in JavaScript. Hence + operator concatenates "1" + 2 + 3 and gives output as 123

Question 6:

What will be evaluated value of calculatedValue for following expression

var calculatedValue = 1+2+"3";

In the above expression, the calculation is started from left to right and 1 is considered as number in JavaScript. Hence + operator adds 1 + 2  and result 3.  Further "3" is considered as string in Javascript. Hence 3 + "3" result to 33.

Question 7:

How to create an array in JavaScript?

You can create Array in JavaScript using new keyword or using array literals as shown below:-

var array1 = ["JavaScript", "Angular JS", "jQuery"];
var array2 = ["JavaScript", true, 1];
var array3 = new Array("JavaScript", "Angular JS", "jQuery"); //array3 contains 3 elements
var array4 = new Array(100)  //an empty array of length 100
Question 8:

How to get natural height and width of image in Javascript?

Using naturalHeight and naturalWidth attribute of Image object you can obtain natural height and natural width of an image.

For example:

var image = document.getElementById("myDemoImage");

alert('width: ' + image.naturalWidth + ' and height: ' + image.naturalHeight);


Natural Width and Natural Height of Image

Question 9:

How to change the style of any element using JavaScript?

You change the style of any HTML element using JavaScript.

The syntax to change any HTML element using JavaScript is given below:

document.getElementById(id) = new style
Question 10:

What is encodeURI() function and explain its usage?

The encodeURI() function is used to encode a URI. This function encodes special characters, except: , / ? : @ & = + $ #

For example:-

var uri=" to make a website using javaScript";
var encodedURI = encodeURI(uri);


You can clearly see that space between words in uri variable is encoded as %20 in encodedURI variable. 

Share interview questions


comments powered by Disqus


Social Media