Thursday 25 July 2013

JavaScript Interview Questions



1)      What is JavaScript?

            JavaScript is a scripting language most often used for client-side web development.

2)       Difference between Java & JavaScript
 Listed are key differences between the Java and JavaScript.

1.       Java is an OOP programming language while Java Script is an OOP scripting language.
2.       Java creates applications that run in a virtual machine or browser while JavaScript code is run on a browser only.
3.       Java code needs to be compiled while JavaScript codes are all in text.
4.       They require different plug-ins.


3)      How do you submit a form using Javascript? 

Use document.forms[0].submit();
(0 refers to the index of the form – if you have more than one form in a page, then the first one has the index 0, second has index 1 and so on).

4)      How to read and write a file using JavaScript? 

I/O operations like reading or writing a file is not possible with client-side JavaScript. However, this can be done by coding a Java applet that reads files for the script.

5)      What are JavaScript types? 

Number, String, Boolean, Function, Object, Null, Undefined.

6)      What is the official JavaScript website?

This is a trick question used by interviewers to evaluate the candidate’s knowledge of JavaScript. Most people will simply say javascript.com is the official website.

The truth is- there is no official website for JavaScript you can refer to. It was developed by Brendan Eich for Netscape. It was based on the ECMAScript language standard; ECMA-262 being the official JavaScript standard.

7)      What is the different between JavaScript and jQuery?

jQuery is a quick as well as concise JavaScript Library that simplifies HTML document traversing, animating, event handling, & Ajax interactions for the purpose of quick web development needs. So although they are not entirely different, both are not the same either!

8)       Explain the strict mode in Javascript.

The strict mode ensures that if functions are not properly thought it, those are disabled. It also kept a check on potentially unsafe actions and throw errors when it happens.

9)       Is it possible for you to write a one line JavaScript code that concatenates all strings passed into a function?

The following function should help in producing the desired result

function concatenate()
{
  return String.prototype.concat.apply('', arguments);
}

10)   Explain JavaScript closures.

A basic overview of JavaScript closures is that it is a stack-frame which is not de-allocated when the function returns.

11)   How do you create a new object in JavaScript?

var obj = new Object(); or var obj = {};


12)   How do you assign object properties?

obj["age"] = 17 or obj.age = 17

13)   What’s a way to append a value to an array?

arr[arr.length] = value;

14)   What is this keyword?

It refers to the current object.

15)  How many looping structures can you find in javascript?

If you are a programmer, you know the use of loops. It is used to run a piece of code multiple times according to some particular condition. Javascript being a popular scripting language supports the following loops

for
while
do-while loop

16)   How to create arrays in JavaScript?

We can declare an array like this
var scripts = new Array();
We can add elements to this array like this
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
Now our array scrips has 4 elements inside it and we can print or access them by using their index number. Note that index number starts from 0. To get the third element of the array we have to use the index number 2 . Here is the way to get the third element of an array.
document.write(scripts[2]);
We also can create an array like this
var no_array = new Array(21, 22, 23, 24, 25);

17)  How about 2+5+"8"?

Since 2 and 5 are integers, this is number arithmetic, since 8 is a string, it’s concatenation, so 78 is the result.

18)   What does "1"+2+4 evaluate to?

Since 1 is a string, everything is a string, so the result is 124.

19)   What is JavaScript namespacing? How and where is it used?

Using global variables in JavaScript is evil and a bad practice. That being said, namespacing is used to bundle up all your functionality using a unique name. In JavaScript, a namespace is really just an object that you’ve attached all further methods, properties and objects. It promotes modularity and code reuse in the application.

20)   What is the difference between “==” and “===”?

 “==” checks equality only,
“===” checks for equality as well as the type.

No comments:

Post a Comment