JavaScript Interview Questions

Q. How to create a class?
JavaScript does not have a class definition. To mimic classes in JavaScript functions can be used to declare a class.
Let’s create a student class in JavaScript which takes two parameters name and roll as property. The code will look like below,
Function Student (name .roll){ = name;
this.roll = roll;

Q. How to create an object?
An object in JavaScript can be created using two was:
New Key word: 
To create a student object from the above student class we can call the Student function using new keyword.
var student1 = new Student(‘sandeep’,2)
Anonymous Object:
Anonymous objects can be created using pair of curls’ braces containing property name and value pairs.
Var rose = {‘color’: ‘red’}

Q. How to declare a private and a public member?
Private members are declared using var keyword and constructor function.
Function Student (name, roll){
var id= ABCD123; = name;
this.roll = roll;
When a Student object vi1l be created the propertied name and roll will be accessible using dot operator but id will not be accessible as it behaves as a private member and return undefined on call.


The above chrome console is showing a student1 object is property is accessible as it is showing sandeep on call. So name is a public property for the student object. But id property is not accessible and returned undefined on call. This shows id is a private property in student1 object.

Q. What is prototype property?
By Using Prototype we can add new members to an existing object. Every JavaScript object has this property internally. Initially it is an empty object.

function Student (name, roll){ = name;
this.roll = roll;
var student1 = new Student(’sangeeta’,30);
Student.prototype.mark = 100;
Checkout the below chrome console for the use of Protype.
Initially the student1 object has only two properties name and roll. By using prototype a new property mark has been added to student object with a value of 100.Now the console shows that the mark property is also added to the existing student1 object.

Q. What is constructor property?
Constructor property of an object maintains a reference to its creator function.
Let us checkout an example by creating a student object and calling the constructor property on it.
function Student( name, mark){
this. name = name;
this. mark =mark;
var student 1 = new Student (’sandeep’, 123);
console.log (student1.constructor);
function Student(name, mark){
this. name = name;
this. mark = mark;
var student1 = new Student(’sandeep’ ,123);
console .log(student1.constructor);
Checkout the following screen shot for above code in chrome console. The console log is printing the referenced function by student1 object.


Q. How to call other class methods?
Using call () and apply () method we can use methods from different context to the current context. It is really helpful in reusability of code and context binding.
call (): It is used to calls a function with a given this value and arguments provided individually.
apply (): It is used to call a function with a given this value and arguments provided as an array.
Below code has two function getTypeOfNumber () and getTypeOfAllNumber. The details pf these functions are below.
getTypeOfNumber: This method takes single number as parameter and return the type either Even or Odd.
getTypeOfAllNumber: This method takes array of numbers as parameter and returns the types in an array with Even or Odd.
var MyNumber = {
getTypeOfNumber: function(number){
var type = (number % 2 === 0)? “Even”: “Odd”;
return type;
getTpeOfAll Number: function O{
var result = [J,i=o:
for (; i < arguments. length; i++){
var type =
MyNumber. GetTypeOfNumber. call(null,arguinents[i]);
result .push(type)
return result;
var typeOfNumber = MvNumber.getTypeOfNumber. call ( null, 21.)
var tvpeOfAflNumber = MvNumber.getTypeOfAllNumber. apply(null [2,4,5,78,21])
Below screenshot shows output of the above code Firebug console.


Q. Explain method overriding with an Example?
We can override any inbuilt method of JavaScript by declaring its definition again. The existing definition is accessible ot override by the Prototype property. Consider the below example, Split () class. But we have overridden its definition using its prototype property.
Below screen shows the inbuilt behavior of split () method. It has divided the string into an array of element.


Below screenshot shows the new overridden definition of split () method. It is normally returns string “I am overridden”.


Q. How to inherit form a class?
Inheritance can be achieved in JavaScript using Prototype property.


We need to follow 2 steps to create an inheritance.

Child class prototype should point to parent class object.
.prototype = new ();

Reset the child class prototype constructor to point self. .prototype .
constructor =

Below example shows ScieneStudent class as a child class of Student class. As the method showMyType () is available for Science Student object.
Function Student (name){;
Student .prototype.savIyTvpe = function(){
consoe.1og (”I am student type”)
function Science Student (name){
Science Student .prototvpe = new Student();
ScienceStudent.prototvpe.constructor = Science Student;
var student2 = new Science Student(sangeeta’);
console.log(student2 .sayMyType ());
Check out the below screen shot for the output of the above code in the developer console.


To test the type of an object belongs to a class or not instanceOf can be used. This returns a Boolean value, TRUE for an object belongs to a class or else FAlSE. Check the below screen shot for the test of student2 object using instanceOf.


Q. What is differential inheritance?
Differential Inheritance is a common prototype-oriented model that uses the concept that most objects are derived from other, more generic objects, and only differ in a few small aspects. Each object maintains a reference to its prototype and a table of properties that are different.

Q. What is Object.create () method do?
ECMAScript 5.1 has this method in its specification. This method can be used to create a new object with given prototype object and properties. The syntax of this method is listed below.
Object. Create (proto [, properties Object])

Below code has a Student class functioned with name property and the prototype object has a method getStudentName () which return the name of the student. A new student1 object has been creates using Object.Create() method by passing the prototype object of Student with value of the name as sandeep. Then the getStudentName() method is called on student1 object and logged in the console.
function Student(name) { = name;
Student.prototvpe = {
getStudentName: function() {
return “Name of Student is :“ +
var student 1 = Object .create(Student .prototype); = “Sandeep”:
console.log(student1 .getStudentName () );
The following screenshot shows the output of the above code in the developer console.

Picture-8 Picture-9

Q. Write a polyfill for Object.create() method if it is not present in the browser?
ECMAScript 5.1 has this method in its specification. If the browser is old then Object.create () method is not present.To resolve this we need to write a polyfill. Below code shows the polyfill for Object. Create () method.
//check if create method is present inside Object
if (tvpeof Object.create ! = ‘function’) {
I/define the create method
Object.create = (function() {
var Object = function() {};
return function (prototype) {
If (argurnents.length> 1) {
throw Error (’Second argument not supported’);
if (arguments. length> 1){
throw Error (‘Second argument not supported’);
if (typeof prototype ! = ‘object’) {
throw TypeError(’Argument must be an object’);
Object. prototype = prototype;
var result = new Object();
Object. prototype = null;
return result;
The above code checks if the create () method is already present inside the Object using if condition and comparing its type to function. If this condition true it means the create () method is not present. Then the polyfill code block gets executes and assigns the empty object to Object. Create property.

Q. What is the purpose of Object.defineProperties () method?
ECMAScript 5.1 provides Object.defineProperties () method to create new properties to a defined object. It provides many configuration options to initialize these members. Below code shows the use of this method.
function Student (name) { = name;
var student 1 = Object .create(Student tvp,
properties ={
“subject”: {
value: “Computer”,
writable: true,
enumerable :true
“marks”: {
value: 0,
writable: false,
enumerable: true
Object. Define Properties (student1, properties); = “Sandeep”:
student 1 .subject = “Mathematics”;
student1 .marks=75;
console.log (student1);
In the above code a student1 object created using Object.create() method. Then some new properties like subject and marks are added to this object. The enumerable option decided whether the property can be enumerated as own property. Writable property decides whether the property is modifiable or not. The value property takes the default value of the property.
Below screenshot shows the output of the above code in the developer console.

Q. What is JavaScript and what does it do?
JavaScript is a scripting language that adds interactivity to HTML pages.

Q. What kind of language does JavaScript provide?
JavaScript is an interpreted language that executes scripts without preliminary compilation.

Q. Is there any connection between Java and JavaScript?
No. They are different in every way and JavaScript is not as powerful and complex as Java.

Q. What is the official name of JavaScript and is it supported by all browsers?
The official name of JavaScript is ECMA (European Computer Manufacturer’s Association) and with it, Internet Explorer 4 and Mozilla Firefox 1.5 fully supported.

Q. What does JavaScript do?
JavaScript is meant to be an easy scripting language that helps the non- programmers with its simple syntax. JavaScript is smart enough that it can put dynamic text into HTML pages, it can react to events (like when a page has finished downloading), and it can read and write HTML elements, create cookies and so forth.

Q. Does prior knowledge of JAVA ease the use of JavaScript?
Yes. Being modeled after Java, which in turn is modeled after C++, JavaScript should be easier to familiarize and work with.

Q. Is JavaScript case sensitive?
Yes. Unlike HTML, JavaScript has to have all variables and function names (etc.) in capital letters.

Q. How do you place JavaScript?
JavaScript may be inserted into code with the following syntax:

Q. Where do you place JavaScript?
JavaScript may be placed in the or section of HTML code, but it is usually a good practice to place it in as to not hinder your code later on.

Q. How do you terminate statements in JavaScript? 
In JavaScript, statements are terminated by semicolons (;) and although they are not mandatory they are a good practice to pick up.

Q. Why are comments used in JavaScript and how are they inserted?
Usually comments are added to make the code more readable but they can also be used to explain the code. They are inserted with // (for single line comments) and /* */ for multiple lines comments.

Q. What are variables and how are they inserted?
Variables are storing containers used for holding expressions and values. They can have a short letter or a longer name and are inserted with the statement: var. Because the variables are loosely typed, they can hold any type of data.

Q. What does a variable of var y=10; and var catname= “Tomcat”; do?
With the execution of the above code, we have variables that hold values of io(for y) and Tomcat (for catname).
Note that the inclusion of text warrants “ “being used.

Q. How many statements types can we find in JavaScript? Give some examples?
The statement types found iii JavaScript are: Expression statements, compound, empty and labeled statements.
Example: break, continue, default, do, for, etc.

Q. What are conditional statements and how are they implemented in JavaScript?
Conditional statements are used to perform and act on different sets of conditions declared by the programmer. They are the following: if statement; if…else statement; if…else if…else statement and the switch statement.

Q. How will you determine a variable type in JavaScript?
A variable type in JavaScript is determined using Typeof operator. When the object is String. Number, Function, undefined and Boolean, the operator returns the same type. And when the object is null and array, the operator returns ‘object”.
var count=100;
type of count;—–à(returns ‘number”)

Q. What is the difference in evaluating [“8”+5+2] and [8+5+2]?
In [“8”+5+2],”8” is a String. So anything that trail the string will be changed to string. Hence the result will be”852”.
In [8+5+”2”],8 and 5 are integer, so it gets added up (13).And “2” is treated as String. Hence the concatenation takes place and the result will be “132”.

Q. Is it possible to assign a string to a floating point variable?
Yes. Any variable can be assigned to another data type. For example.
var a1= 10.39;
document. Write (a1);—à 10.39
document. Write(aij;—-à hai

Q. Will variable redeclaration affect the value of that variable?
No. The same value will be retained in the variable.
var status=”cool”;
document. write (status”); //cool
var status;
documeiit.ivrite(’status”); //cool
status= “chill;
document.w’rite(status”); //chill

Q. How will you search a niatcliing pattern globally in a string?
A matching pattern can be globally searched in a string using “g” modifier in Regular Expression.
var p1 = “ First_Regular_ Expression_First”;
var q1=”/First/g”;
document.write(”Pattern_Match:”+ p1 .match(q1)); //
Pattern_Match: First, First

Q. How will you search a particular pattern in a string?
A particular pattern in string can be searched using test function. If the match is found it returns true, else false.
var my_pattern1= new RegExp(”pp”);
document. write(my_pattern 1. Test (“Happy_Days”); //true

Q. Which property is used to match the pattern at the start of the string?
“^” symbol is used for position matching.
var pi =First_Regu1ar_ Expression_First”;
var qi=’/First/^”;
document.write(Pattern.Match:” + p1.match(q1));

Q. Which property is used to match the patterii at the end of the string?
“$” symbol is used for end position matching.
var p1=”First_Regular_ Expression_First”;
var q1=First/$”;
document .write(”Pattern_Match:”+ p1. match(q1));
// Pattern_Match: First—à Expression_First

Q. What are operators? Which are the most important operators in JavaScript?
Operators in JavaScript are used to combine values that form expressions. The most important are: = and +. The first is used to assign values and the second one is used to add values together.

Q. Why comparison and logical operators are used?
Comparison operators are used to determine if there is a difference between variables, and also their equality, while the logical operators are used to determine the logic of variables.

Q. How many types of pop-up boxes does JavaScript have? What are those?
JavaScript has three types of pop-up boxes and they are: alert, confirm and prompt.

Q. Does creating an alert box prompt the user to respond sit1i OK or Cancel?
No. An alert box only gives the user the option of choosing OK to proceed.

Q. What are functions in JavaScript and where are they placed?
Functions contain code that is executed before an event thus stopping the browser from loading a script when the page opens. Functions can be placed both in the or section, but it is advised to place them in the section.

Q. What does the keyword null mean in JavaScript?
The keyword null is a special value that indicates no value. It is unique from other values and also fully distinct.

Q. What does the value undefined mean in JavaScript?
Undefined is a special value in JavaScript, it means the variable used in the code does not exist or is not assigned an’ value or the property does not exist.

Q. Do the null and undefined values have the same conversion in Boolean, numeric and string context?
No. The undefined value changes into Nan in numeric context and undefined in a string context. They share the same conversion in Boolean.

Q. What are Boolean values?
Boolean values are data types that only have two types of values: true or false; a value of Boolean type only represents the truth: it says if it true or not.

Q. Can a Boolean value be converted into numeric context?
Yes. If it is converted into numeric context the true value becomes 1 and if it is a false value it becomes a 0.

Q. What happens when a number is dropped where a Boolean value is expected to be?
The number is converted into a true value, but on1y if it not equal to o or NaN which in turn is converted into a false value.

Q. What are objects in JavaScript?
Objects are collections of named values that most of the times are referred to as properties of an object.

Q. What is all array in JavaScript?
An array is a collection of data values which can handle iiiore than one value at a time, tile difference being that each data in an array has a number or index.

Q. From which version forward has JavaScript stopped using ASCII character set?
From V.3 JavaScript started using Unicode character sets: identifiers can now contain letters and digits from the Unicode complete character set.

Q. What is the scope of a variable in JavaScript?
The scope of a variable is the region in which your program in which it is defined. Thus a global variable has a global scope meaning it is defined everywhere in your JavaScript code. The local variables have a local scope meaning they are defined only in the body section of your code.

Q. In the body of the code which variable with the same iiame has iiiore importance over the other: the local or the global variable?
In the body of a function a local variable will always take precedence over a global variable hiding it all together.

Q. How many types of undefined variables can we find in JavaScript?
There are two kinds of undefined variables: the first is the one that has never been declared and the second is the kind of undefined variable that has been declared but has never had a value assigned to it.

Q. What are the (==) (===) and what do they do in JavaScript?
The first (==) is the equality operator and it checks if its two operands are equal. The second (===) is called the identity operator and it checks if two operands are identical by using a strict definition.

Q. How many types of operators can we find in JavaScript?
There are eight types of operators in JavaScript. These are as follows:
operator, arithmetic, equality, relational, string, logical, bitwise and miscellaneous.

Q. How many types of comparison operators does JavaScript contain?
There are four types of comparison operators in JavaScript. They are:
less than (<); greater than (>); less than or equal (<=) and greater than or equal (>=).

Q. Can comparison be done on any type of operands?
Yes. Operands in JavaScript that are not number or strings are converted.

Q. What are logical operators and how arc they used in JavaScript?
The logical operators perform Boolean algebra and are used mostly with comparison operators to show complex comparisons that involve more than one variable.

Q. Does JavaScript contain classes?
Yes. Although they do not define the structure of an object like in Java and C++, it does approximate the classes with its constructors and their prototype objects.

Q. What is an object in JavaScript?
An object is an instance of its class. This allows us to have multiple instances of any class.

Q. How are classes and objects in JavaScript named and why?
Classes are named with an initial capital letter and an object with lowercase letters. This ensures that classes and objects are distinct from each other.

Q. What are class properties in JavaScript?
Class property is associated with a class itself and not instance of a class. This ensures that no matter how many instances of a class are created only one copy of each class property exists.

Q. What are class methods in JavaScript?
Class methods are associated with a class rather than an instance of a class, meaning they are invoked by the class itself and not an instance of the class.

Q. Do class properties and class methods have a global and local range?
No. They are both only global because they do not operate on a particular object.

Q. How do JavaScript equality operators compare objects?
Equality operators compare objects by reference and not by value, checking to see if both references are to the same object. They do not check to see if two objects have the same property names and values.

Q. Are the utah and undefined values for the variable saute?
No. Variables that are declared and not assigned any value will have undefined values whereas the variable that is assigned a null will have null values.
var qno;—à “undefined value”
var Items=10;
Iteius=null; —à” null value”

Q. What is the difference between “= = =” and “= =“?
a) ===‘ returns true – if both the operands are same and has same data type
b) “==“ checks only for operands – if both are same, returns true
For comparison of operands. JavaScript converts different data type to same type.
Let p= 3
p==’3’ //true p===’3’ //false
3==’3’ //true 3===’3’ // false
P==3 I/true P===3 //true

Q. What is the function of delete operator in JavaScript?
It deletes
a) an object
Syntax: delete obj_name 1;
b) particular element in an array
Syntax: delete arr_ele1[index];
c) property of an object
Syntax: delete obj_name2 .prop_name;

Q. How will you clip the particular portion of an element?
Using clip property of the style object, we can clip a particular portion of an element.



Get Updates on Tech posts, Interview & Certification questions and training schedules