Thursday, 9 April 2015

JavaScript Interview Questions And Answers.

1. What is JavaScript?

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

2. What are JavaScript types?

Ans: a) Number
b) String
c) Boolean
d) Function
e) Object
f) Null
g) Undefined

3. What is the difference between “==” and “===”?

Ans: == is compre the values and === is compre the values as well as type.
ex: var x="4";
            var y=4;

           x==y returns true;
          x===y treturns false;

4. What are the way of make comment in Javascript?

Ans: There are 2 types of comments in javascript
      a) single line comment.( ex: // your code ).
      b) multiline comment (ex: /* your code
  morethen one line */)

5. How you will get the CheckBox status whether it is checked or not?

Ans: var status = document.getElementById('checkbox1').checked;
alert(status);
it will return true or false.

6. What does "1"+2+4 evaluate to?

Ans: Since 1 is a string, everything is a string, so the result is 124. string+integer is string

7. What does 1+2+"4" evaluate to?

Ans: step1: 1+2 is 3
       step2: 3+"4"
       result is 34 because integer + string returns string.

8. How do you change the style/class on any element using javascript?

Ans: document.getElementById(“myText”).style.fontSize = “10";
-or-
document.getElementById(“myText”).className = “anyclass”;

9. What is ‘this’ keyword in JavaScript?

Ans: 'this' is a keyword used to point the corrent object in the code .

10. What is the function of delete operator?

Ans: The functionality of delete operator is used to delete all variables and objects in a program but it          cannot delete
        variables declared with VAR keyword.

11. What is the use of Void(0)?

Ans: Void(0) is used to prevent the page from refreshing and parameter “zero” is passed while calling.
Void(0) is used to call another method without refreshing the page.

12. How to create arrays in JavaScript?

Ans:There are two ways to create array in JavaScript like other languages:
    a) The first way to create array
Declare Array:
EX:var names = new Array(); or var names = [];
Add Elements in Array:-
names[0] = “bujji”;
names[1] = “babu”;

  b) This is the second way:
var names = new Array("bujji", “babu”);

13) How do you submit a form using JavaScript? 
 
Ans: Use document.forms[0].submit();

14) What does isNaN function do?

Ans: It returns true if the argument is not a number.
     if argument is a number is return false

15.What is DOM? What is the use of document object?

Ans: DOM stands for Document Object Model. A document object represent the html document. It can be used to access and change the content of html.

16. What is the use of window object?

Ans: The window object is automatically created by the browser that represents a window of a browser.
    It is used to display the popup dialog box such as alert dialog box, confirm dialog box, input dialog box etc.
    Window is the object of browser, it is not the object of javascript.
   using window object we can modify the global variables.

17.What is the use of history object?

Ans: The history object of browser can be used to switch to history pages such as back and forward from current page or another page. There are three methods of history object.
    history.back()
    history.forward()
    history.go(number): number may be positive for forward, negative for backward.

18.Difference between ‘undefined’ and ‘null’

Ans:
    Undefined:
     In JavaScript, undefined is a variable with no value.
     The typeof a variable with no value is also undefined.
     ex: var x  //Value is undefined, type is undefined
    Null:
     In JavaScript null is "nothing". It is supposed to be something that doesn't exist.
     Unfortunately, in JavaScript, the data type of null is an object.
    ex: var y = null;   // Value is null, but type is still an object
 Note: x==y returns true;
       x===y returns false.

19.  what is javascript clouser? 

Ans:A closure is a function having access to the parent scope, even after the parent function has closed.
 Ex: var add = (function () {
    var counter = 0;
    return function () {return counter += 1;}
})();
add();
add();
add();

The variable add is assigned the return value of a self invoking function.
The self-invoking function only runs once. It sets the counter to zero (0), and returns a function expression.
This way add becomes a function. The "wonderful" part is that it can access the counter in the parent scope.
This is called a JavaScript closure. It makes it possible for a function to have "private" variables.
The counter is protected by the scope of the anonymous function, and can only be changed using the add function.

2 comments: