Thursday, 20 February 2014

Java Script Interview Question Answer

How do we add JavaScript onto a web page?

Ans:There are several way for adding JavaScript on a web page, but there are two ways which are commonly used by developers
 Code
<head> <title>Page Title</title> <script language="JavaScript" type="text/javascript"> var name = "Vikas Ahlawta" alert(name); </script> </head> 
 

Ques-Is JavaScript case sensitive?

Ans:Yes!A function getElementById is not the same as getElementbyID. 
  

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

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

What are the ways of making comments in JavaScript?

Ans:  // is used for line comments
ex:- var x=10; //comment text

/*
*/  is used for block comments
 

How will you get the Checkbox status whether it is checked or not?  

var status = document.getElementById('checkbox1').checked; 
alert(status); 
 

How to create arrays in JavaScript? 

There are two ways to create array in JavaScript like other languages:
a) The first way to create array
Declare Array:
           var names = new Array(); 
Add Elements in Array:-
names[0] = "Vikas";
names[1] = "Ashish";
names[2] = "Nikhil";
 
b) This is the second way: 
   var names = new Array("Vikas", "Ashish", "Nikhil");
 

If an array with name as "names" contain three elements, then how will you print the third element of this array?  

Print third array element document.write(names[2]);
Note:- Array index starts with 0.
 

How do you submit a form using JavaScript?  

Use document.forms[0].submit();

What is an object in JavaScript, give an example? 

An object is just a container for a collection of named values:
Example-
var man = new Object();
man.name = 'Vikas Ahlawat';
man.living = true;
man.age = 27;
 

How you will add function as a property in a JavaScript object? Give an example.  

var man = new Object();
man.name = 'Vikas Ahlawat';
man.living = true;
man.age = 27;
man.getName = function() { return man.name;}
console.log(man.getName()); // Logs 'Vikas Ahlawat'.
 

Does JavaScript Support automatic type conversion, If yes give example.

Yes! Javascript support automatic type conversion. You should take advantage of it, It is most common way of type conversion used by Javascript developers. 
Ex
var s = '5';
var a = s*1;
var b = +s;
typeof(s); //"string"
typeof(a); //"number"
typeof(b); //"number"
 

No comments:

Post a Comment