Advanced JavaScript Interview Questions and Answers

Advanced JavaScript Interview Questions and Answers

Today there are plenty of vacancies for JavaScript developers. If you are one among them who is searching a job for JavaScript, I can suggest you to take a look at the below JavaScript interview Questions and Answers. Hope this will help you in your Interview.

Years back JavaScript is one of the famous client-side Scripting language. In web development JavaScript helps to reduce load on server by doing minimal jobs at client end. Lets take an example where and how JavaScript works.

Assume in a web page we have an input field where user need to provide his/her email id for subscription. Before take the user email id into our customers database we need to verify was the user provided a valid email id. Else it is a fake record to our storage. To do this we have many methods in server-side scripting but by checking a valid email id in server it uses server resource as well it increases the load time. Even in real-time a busy user need to wait for server response to verify email validation. Here client-side script takes role. JavaScript compiler resides in Client browser and it can check any email validation with out using server-side scripting. This helps to develop more better performance based web applications.

What are the different data types in JavaScript?

JavaScript has Dynamic Data types. The different types of Data types JavaScript supports are String, Number, Array, Object, Boolean, Null & Undefined. JavaScript is so smart. Using var only we can create any kind of Data types. Look at the example below.

//String Datatype
var str = “this is a string datatype”;
//Number Datatype
var num = 34;
//Boolean Datatype
var bool = true;
//Array Datatype
var arr = [“rosa”, “rupak”, “rohini”];
//Object Datatype
var obj = { name: “biswabhusan”, age: 33, designation: “Team Leader”};
//Undefiend Datatype
var und;
//Null Datatype
var noval = null;

How to Create an Object, Properties & Methods using JavaScript?

JavaScript is a client-side technology. It supports Object Oriented Programming feature. In JavaScript almost everything is an Object. String, Dates, Functions & Array are the example of JavaScript objects. You can create your own object in JavaScript. There are several ways to do this.

In this block I am presenting 2 ways to create object in JavaScript. Let us talk about an employee object. For the below employee object we have the properties like first name, last name, age & designation.

var employee = {
firstName:"John",
lastName:"Smith",
age:40,
designation:"Team Lead"
};

OR

employee = new Object()
employee.firstName = "John"
employee.lastName = "Smith"
employee.age = 50
employee.designation = "Team Lead"

Now in the above employee object I need to implement a method which will return full name of the employee. Look at the below example.

var employee = {
firstName:"John",
lastName:"Smith",
age:40,
designation:"Team Lead",

getFullName : function() {
alert(employee.firstName + " " + employee.lastName);
}
};

Here getFullName is the method using which I am extracting full name of an employee. Like the above examples you can create Object, Properties & Methods in JavaScript.

What is closures in JavaScript?

Before make you clear about closures in JavaScript. Let us discuss the variable scope in JavaScript. Using JavaScript we can declare a variable either local or in global Scope. Variables in local Scope mean we can use this variable inside that function only, where it is declared. In-case of global variables we can use it in any functions. Lets take an example.

Example of Local Scope Variable

function demoFunc() {
//Local variable b
var b = 5;
return b * b;
}

Example of Global Scope Variable

//Global variable b
var b = 5;
function demoFunc() {
return b * b;
}

Now to make you understood closures in JavaScript let us discuss one practical programming problem. Suppose you want a use a function increaseCounter() to increase 1 by 1 in each call. In this function what we are doing, we are increasing a global variable by 1 in each of this function call.

var counter = 0;

function increaseCounter() {
counter += 1;
}

increaseCounter();
increaseCounter();

In this case without the function call also we can update the counter variable as its in Global Scope. If I am making the counter variable to local scope this function will reset the counter in each call. Here JavaScript closures works. See the example below.

Example of using JavaScript closures

var increaseCounter = (function () {
var counter = 0;
return function () {return counter += 1}
})()

increaseCounter();
increaseCounter();

Is JavaScript is case sensitive?

Yes, JavaScript is case sensitive. Lets talk about a DOM object getElementById is not same as getelementbyid.

What is the difference between “==” & “===” in JavaScript?

== & === both are used to Compare equality. Triple equal doing additional checking for variable types. Look at the example below.

var a = 0;
var b = "0";

if (a==b){
alert('The value of a & b are same.');
}

if (a===b){ } else {
alert('Data type mismatch for a & b.');
}

How to comment in JavaScript?

In JavaScript comments can be added using 2 ways. One is Line Comment. This can be done using 2 forward slashes (//). For block commenting use /*………………..*/.

Example of JavaScript Commenting

// This is a Line Comment in JavaScript
var a = 0;
var b = "0";

/* This is block Comment in JavaScript
if (a==b){
alert('The value of a & b are same.');
}

if (a===b){ } else {
alert('Data type mismatch for a & b.');
}*/

How to know Checkbox Status using JavaScript?

Let we have a HTML Checkbox Control in our page with the id “chkTemp”. To check the Status of this check box we can write the following JavaScript.

var ChkStatus = document.getElementById('chkTemp').checked;
document.write(ChkStatus);

How to Create Array using JavaScript?

With JavaScript Syntax there are 2 ways to declare an Array. Look at the examples below.

// First way to Declare JavaScript Array
var empName = new Array();
empName[0] = "Rajesh Sukla";
empName[1] = "Rakesh Srivastab";
empName[2] = "Ritesh Deshmukh";

// Second way to Declare JavaScript Array
var empName = new Array("Rajesh Sukla", "Rakesh Srivastab", "Ritesh Deshmukh");

What isNaN function does in JavaScript?

isNaN function does number checking in JavaScript. It returns true if the argument doesn’t seems to be a Number.

How to Change Style or Class of an element using JavaScript?

Look at the example below here I declared a div with id txtSample. Using Document Object Model here I am extracting the div control by its id. Applying style font-size 20px on div. In the next line of code I am assigning a css class to my div with the name anyclass.

document.getElementById("txtSample").style.fontSize = "20px";
document.getElementById("txtSample").className = "anyclass";

What is Prototypes in JavaScript?

Using Prototypes in JavaScript we can define methods to all instance of an object. The method applied to prototypes stored in memory. With each instance of the object we can access it. Lets watch the following example.

function Emp(name, designation){
    this.name = name;
    this.designation = designation;
}
function view(){
    return this.name + " is a " + this.designation + "!";
}
Emp.prototype.view = view;
var emp1 = new Emp('Biswabhusan', 'Module Lead');
alert(emp1.view());

//Outputs "Biswabhusan is a Module Lead!"

Tell me the difference between Undefined & Null?

When Null returns an object Undefined returns nothing or Undefined. Undefined means that the variable has not been declared or initialized. By declaring null to a variable it contains empty value but it is initialized. Let’s take an example. In my app I referred the CDN link of Jquery library. If using typeOf I am getting typeOf(jquery) undefined it means CDN link of jquery fails to load. While declaring a variable it’s wise to initialize the variable with Null value.

Why we use splice() Method in JavaScript?

Splice() Method is used to manipulate JavaScript array. The syntax is Array.splice(index, how many, item 1,…..,item N). Here index & how many is required parameters. Where items are optional. Index accepts numbers from which position we want to add or remove array elements. How many says how many number of elements we want remove from the supplied index value. Item 1 to N adds this values after the index position.

To make your understanding clear please find the below example.

/* Inserting one element to the existing array using Splice method */
var fruits = ["A", "B", "C", "D"];
fruits.splice(2, 1, "E", "F");

Output after splice - A, B, E, C, D

/* Applying only splice method not inserting any elements */
var fruits = ["A", "B", "C", "D"];
fruits.splice(2, 2);

Output after splice - A, B

Source of Image: http://www.careerealism.com