Saturday, March 25, 2017

JavaScript Interview Questions

In this post I am sharing JavaScript Interview Questions.JavaScript is light weight and client side scripting language that can be inserted into HTML pages and is understood by web browsers.This scripting language produced by NetScape for use withing the HTML web pages.JavaScript has almost nothing to do with the programming language named java. Java and JavaScript are two different two langauges.Java is powerful object oriented programming languages like C++,C# whereas JavaScript is a client-side scripting language with some limitations.Web browsers are not only platforms on which Java Script is used. Some databases such as MongoDB use Java Script their scripting and Query Language.

Now we will Read technical Interview Questions for Java Script:

1) What is Java Script?

Ans: Java Script is a light weight and client side scripting language that can be inserted into HTML web pages and is understand by web browsers.

2) What is Difference between Java and Java Script?

Ans:  These two languages are not at all inter-dependent and are designed for different purpose.Java is pure object oriented programming language whereas Java Script is Client side scripting language and it is unstructured programming.

3) How to we add JavaScript onto a webpage?

Ans: There are several ways for adding JavaScript on a web page,but there are two ways which are commonly used by developers. If your script code is very short and only for single page,then the follow the below steps:

a) 
<html>
<head>
<title>adding js in <head> tag</title>
<script type='text/javascript'>
var name="Raju";
alert(name);
</script>
</head>
<body>
....................
.....................
</body>
</html>

b) If your script code is very large,then you can make a java script file and add its path in the following way:
<html> <head>
<title>page title</title>
<script type="text/javascript" src="demo.js"></script>
</head>
<body>
...............
................
</body>
</html>

4) What are the types in JavaScript?

Ans: In JavaScript we have
String
Number
Boolean
Function
Object
Null
Undefined

5) What is Difference between null and undefined?

Ans: The variable which doesn't have value is called undefined.
The variable can have the value null(empty value). It will not return undefined.

6) What is difference between == and ===?

Ans: The identify operator(==) is also known as equality operator(==). The difference is identify operator will compare both value as well as type of both variables.But,the equality operator will check whether both variables are having same value or not.

Then you thing that which one is best, the identify operator will be faster than the equality operator because identify operator do not check for the type conversion.

Examples: 
0==0 //true
0===0 //false

7) Which is faster between JavaScript and ASP?

Ans: JavaScript is is faster. Since it is a client-side language and thus it does need the assistance of the web server to execute whereas ASP is a server side language and hence always slower than JavaScript.

8) What is this keyword in Js?

Ans: 'This' keyword normally refers to the object that own the method,but it depends on how a function is called. The main purpose of 'This' keyword is to points to the current scope object that owns where you are in the code.
Example: When working with event handlers,JavaScript's this keyword will point to the object that generated the event.
function myFunction(){
this.name="Rama";//here this keyword points to the function myFunction()


9) How foreach loop works in JavaScript?

Ans: JavaScript also supports foreach loop like as Java. But in JavaScript we will call it as for-in statement. This loop will continue until the value exist in the array or object.

Example:

var arr=[10,20,30,40,50];
for(index in values)
{
console.log(values[index]);//10,20,30,40,50
}

10) What is use of instanceof operator?

Ans: In JavaScript we can determine the object instance for the specific constructor using the operator instanceof.

11) How would you create the instance for objFunction in below code?

Ans: we can create the instance of the function objFunction using the keyword new as shown in the below example:

function objFunction(){
//statement goes here........
}
var obj=new objFunction();

12) What is an Array in JavaScript?

Ans: An array is a variable that can store multiple values instead of listing items individually,each with their own variable.
We can create Array in three ways as shown in below examples:

1) var arrayval=new Array();//here we can add n number of index value like arrayval[0]='value1'

2) var arrayval=[];//arrayval[0]='value1'

3) var arrayval=new array(3);//here we put the value limit to 3.so it will create undefined for first three value,until we assign value to that.

13)How would you create empty object in JavaScript?

Ans: There are two ways to create empty object in JavaScript. They are

var obj=new Object();
var obj={};
we can add the properties later once the empty object is created.

14) What is Scoping in JavaScript?

Ans: JavaScript has a lexical scoping based on the function but not based on the blocks. The following  example shoes the scoping:

//global scope
(funtion(){
//anonymous function scope
var value=1;
function obj(){
//obj function scope
var value=2;
}
obj();
console.log(value);//outputs 1
}
if(true)
{
var value=3;//re-declares value
}
console.log(value);//output 3
})();

15)What is DOM in JavaScript?

Ans: The DOM acts as an API for HTML which defines HTML properties,events,and methods. It also refers to HTML elements as objects.

JavaScript relies on this DOM to alter the elements and attributes of a page,and create the dynamic websites.

16)In JavaScript,objects passed by reference or by value?

Ans: In JavaScript all objects are passed by reference.Primitive types(String,Number,Boolean,null and undefined)are passed by value.

17) Is it possible to create private variables using object literals?

Ans: No,we can't create private variables using object literals. All the property of an object literals are public. We can create the private variable using function only.

18) How do you check if a variable is an object?

Ans: we can use typeof to determine the type of the variable. 

Example: 
var value=null;
if(value && typeof value=='object')
{
console.log("the value is an object");
}
else if(value==null && typeof value=='object')
{
console.log("the value is not an object");
}

Note: null is also an object

19) How to handle errors in JavaScript?

Ans:  We can handle runtime exceptions by using try and catch statements. Run the code you want to test in the try block,and if a runtime error occurs,it will execute the code designed in the catch block.

Example:
try{
//the code for try block
}
catch(err)
{
//the code to handle the runtime errors 
}
20)How many types of errors supported by JavaScript?

Ans: There are many errors that can occur in a system,JavaScript provides a message for all the errors at run time and dynamically generate the error code.
Some of the errors such as Load-time errors,Run-time errors and Logic errors.

21)What is pop() method in JS?

Ans: The pop() method takes the last element off the given array and it returns the removed value as shown in below example

var subjects=["hindi","english","maths"];
console.log(subjects.pop());//maths
console.log(subjects);//['hindi','english']

22) What is BOM in JS?

Ans: The Browser Object Model(BOM) allows JavaScript to talk to the browser. The Window object is supported in all browsers. Basically it points to the browser.

Document object is the property of the window object.

23)When onload and onunload events are used in JavaScript?

Ans: The onload and onunload events are triggered when the user enters or leaves the page.

The onload events can be used to check the visitor's browser type and browser version,and load the proper version of the web page based on the information

Both onload and onunload events can be used to deal with cookies.

24) What is Cookie?

Ans: A cookie is a variable that is stored on teh visitor's computer. Each time the same computer requests a page with a browser,it will send the cookie also. In JavaScript,you can retrieve the cookie as well as create the cookie value.

25) Can we use innerHTML in JS?

Ans: YES, we can use but there are some disadvantages are there,it will replace the existing content with the new content. SO you can't append the content using innerHTML. If you want append the HTML to that element,you need to concatenate the new content with the existing content.

Read Also:

IT BASICS FOR TECH DEVELOPERS
Basic Java Interview Questions
How Cookies works in Servlets
What is JavaBean? Where we used?



No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...