Javascript Interview Questions

Tuesday 9 July 2013



·         What is JavaScript?
JavaScript is a platform-independent,event-driven, interpreted client-side scripting language developed by Netscape Communications Corp. and Sun Microsystems.

JavaScript is a general-purpose programming language designed to let programmers of all skill levels control the behavior of software objects. The language is used most widely today in Web browsers whose software objects tend to represent a variety of HTML elements in a document and the document itself.

But the language is used with other kinds of objects in other environments. For example, Adobe Acrobat Forms uses JavaScript as its underlying scripting language to glue together objects that are unique to the forms generated by Adobe Acrobat.

Therefore, it is important to distinguish JavaScript, the language, from the objects it can communicate with in any particular environment.

When used for Web documents, the scripts go directly inside the HTML documents and are downloaded to the browser with the rest of the HTML tags and content.

·         How is JavaScript different from Java?

Don't be fooled by the term Java in both. Both are quite different technologies.

JavaScript was developed by Brendan Eich of Netscape; Java was developed at Sun Microsystems. While the two languages share some common syntax, they were developed independently of each other and for different audiences. Java is a full-fledged programming language tailored for network computing; it includes hundreds of its own objects, including objects for creating user interfaces that appear in Java applets (in Web browsers) or standalone Java applications. In contrast, JavaScript relies on whatever environment it's operating in for the user interface, such as a Web document's form elements.
JavaScript was initially called LiveScript at Netscape while it was under development. A licensing deal between Netscape and Sun at the last minute let Netscape plug the "Java" name into the name of its scripting language. Programmers use entirely different tools for Java and JavaScript. It is also not uncommon for a programmer of one language to be ignorant of the other. The two languages don't rely on each other and are intended for different purposes. In some ways, the "Java" name on JavaScript has confused the world's understanding of the differences between the two. On the other hand, JavaScript is much easier to learn than Java and can offer a gentle introduction for newcomers who want to graduate to Java and the kinds of applications you can develop with it.

·         Difference between JavaScript and Jscript?
Ans:-Both JavaScript and Jscript are almost similar. Java script was developed by Netscape. Microsoft reverse engineered Javascript and called it Jscript

·         Are javascript and jQuery different?

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!

·         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.

·         What’s relationship between JavaScript and ECMAScript?
ECMAScript is yet another name for JavaScript (other names include LiveScript). The current JavaScript that you see supported in browsers is ECMAScript revision 3.

·         What is negative infinity?
It’s a number in JavaScript, derived by dividing negative number by zero.

·         Is it possible to check if a variable is an object?
Yes, it is possible to do so. The following piece of code will help achieve the same.

if(abc && typeof abc === "object") {
console.log('abc is an object and does not return null value');
}

·         Can you explain what isNaN function does?
isNaN function will check an argument and return TRUE (1) if the argument does not seem to be a number.

·         How do you convert numbers between different bases in JavaScript?
Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F", 16);

·         What is the difference between undefined value and null value?
undefined means a variable has been declared but has not yet been assigned a value. On the other hand, null is an assignment value. It can be assigned to a variable as a representation of no value.

Also, undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.
Unassigned variables are initialized by JavaScript with a default value of undefined. JavaScript never sets a value to null. That must be done programmatically.

·         How do we add JavaScript onto a web page?
There are serveral way for adding javascript on a web page but there are two way with is commonly userd by developers
If your script code is very short and only for single page then following ways is best
a)You can place <script type="text/javascript"> tag inside the <head> element.
Code:
<head>
<title>Page Title</title>
<script language="JavaScript" type="text/javascript">
   var name = "Vikas Ahlawta"
   alert(name);
</script>
</head>
b).If your script code is very large then you can make a javascript file and add its path in the following way..
Code:
<head>
<title>Page Title</title>
<script type="text/javascript" src="myjavascript.js"></script>
</head>
·         Is JavaScript case sensitive?
Yes!
A function getElementById is not the same as getElementbyID

·         What are the types used in JavaScript?
Ans:-String, Number, Boolean, Function, Object, Null, Undefined.

·         What are the boolean operators sported by JavaScript?
And Operator: &&
Or Operator: ||
Not Operator: !

·         Differentiate between “var a=2” and “a =2”?
The major difference between the two is that one variable is local and the other is global. “var” basically defines the scope of the variable.

When we add var to a variable value assignment, javascript ensures that the variable is confined to whichever function it is assigned to and does not collide with the same name variable within another function.

When we don’t use var, then it is declared as a global function and chances of collision can happen. So it’s always advisable to use “var” before variable value assignment. If needed use an anonymous function for closure.

·         What is the difference between “==” and “===”?
“==” checks equality only,
“===” checks for equality as well as the type.

·         How to access the value of a textbox using JavaScript?
ex:-
Code:
<!DOCTYPE html>
<html>
<body>
Full name: <input type="text" id="txtFullName" name="FirstName" value="Vikas Ahlawat">
</body>
</html>

There are following way to access the value of the above textbox
var name = document.getElementById('txtFullName').value;
alert(name);
or
we can use the old way
document.forms[0].mybutton.
var name = document.forms[0].FirstName.value;
alert(name);
Note:- this uses the "name" attribute of the element to locate it.

·         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);
}
·         What are the way of make comment in Javascript?
// is used for line comments
ex:- var x=10; //comment text

/*
*/ is used for block comments
ex:-
var x= 10; /* this is
block comment example.*/

·         How you will get the CheckBox status whether it is checked or not?
var status = document.getElementById('checkbox1').checked;
alert(status);
it will return true or false

·         How to create arrays in JavaScript?
There are Two way dor create array in Javascript like other languages..
a) first way to create array
Declare Array:-
Code:
var names = new Array();
Add Elements in Array:-
names[0] = "Vikas";
names[1] = "Ashish";
names[2] = "Nikhil";

b) this is second way
var names = new Array("Vikas", "Ashish", "Nikhil");

·         If an array with name as "names" contain three elements then how you will print the third element of this array?
Print third array element document.write(names[2]);
Note:- array index start with 0

·         How do you submit a form using Javascript?
Use document.forms[0].submit();

·         What does isNaN function do?
It Return true if the argument is not a number.
ex:-
Code:
document.write(isNaN("Hello")+ "<br>");
document.write(isNaN("2013/06/23")+ "<br>");
document.write(isNaN(123)+ "<br>");

output will be:-
true
true
false
·         How to get value from RadioButtonList control?
Here id is the name property of the RadioButtonList
function GetRadioButtonValue(id)
{
o    var radio = document.getElementsByName(id);
o    for (var ii = 0; ii < radio.length; ii++)
o    {
o    if (radio[ii].checked)
§  alert(radio[ii].value);
o    }
}

·         How to detect the operating system on the client machine?
In order to detect the operating system on the client machine, the navigator.appVersion
string (property) should be used.

·         What is the use of Math Object in Javascript?
The math object provides you properties and methods for mathematical constants and functions.
ex:-
Code:
var x = Math.PI; // Returns PI
var y = Math.sqrt(16); // Returns the square root of 16
var z = Math.sin(90);    Returns the sine of 90

·         What do you understand by this keyword in javascript?
“this" keyword refers to the current object like other language.

·         What does "1"+2+4 evaluate to?
Since 1 is a string, everything is a string, so the result is 124.

·         What does 3+4+"7" evaluate to?
Since 3 and 4 are integers, this is number arithmetic, since 7 is a string, it’s concatenation, so 77 is the result.

·         How do you change the style/class on any element using javascript?
Code:
document.getElementById(“myText”).style.fontSize = “10";
-or-
document.getElementById(“myText”).className = “anyclass”;

·         Does javascript support foreach loop?
No.
·         What looping structures are there in JavaScript?
for, while, do-while loops

·         what is an object in JavaScript, give an example?
An object is just a container for a collection of named values

// Create the man object
Code:
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 example.
Code:
var man = new Object();
man.name = 'Saurabh Hangotra';
man.living = true;
man.age = 27;

man.getName = function() { return man.name;}
console.log(man.getName()); // Logs 'Saurabh Hangotra'.

·         What is the similarity between 1st and 2nd statement?
1st:- var myString = new String('Saurabh'); // An object.
2nd:- var myStringLiteral = 'Saurabh'; // Primitive string value, not an object.
Ans:- Both will call String() constructor function
you can confirm it by run the following statement
console.log(myString.constructor, myStringLiteral.constructor);

·         What will be the output of the following statements?
Code:
var myString = 'Saurabh' // Create a primitive string object.
var myStringCopy = myString; // Copy its value into a new variable.
var myString = null; // Manipulate the value
console.log(myString, myStringCopy); 

Ans:- // Logs 'null Saurabh'

·         Consider the following statements and tell what would be the output of the logs statements?
var price1 = 10;
var price2 = 10;
var price3 = new Number('10'); // A complex numeric object because new was used.
console.log(price1 === price2);
console.log(price1 === price3);
Ans:-
console.log(price1 === price2); // Logs true.
console.log(price1 === price3); /* Logs false because price3 contains a complex number object and price 1
is a primitive value. */

·         What would be the output of the following statements?
var object1 = { same: 'same' };
var object2 = { same: 'same' };
console.log(object1 === object2);
Ans:- // Logs false, JavaScipt does not care that they are identical and of the same object type.
When comparing complex objects, they are equal only when they reference the same
object (i.e. have the same address). Two variables containing identical objects are not
equal to each other since they do not actually point at the same object.

·         What would be the output of the following statements?
Code:
var object1 = { same: 'same' };
var object2 = object1;
console.log(object1 === object2);
Ans:- // Logs true

·         What is this?
var myArray = [[[]]];
Ans:- Three dimantional array

·         Name any two JavaScript functions which are used for convert nonnumeric values into numbers?
Ans:-
Number()
parseInt()
parseFloat()
Code:
var n1 = Number(“Hello world!”); //NaN
var n2 = Number(“”);             //0
var n3 = Number(“000010”);       //10
var n4 = Number(true);           //1
var n5 = Number(NaN);            //NaN

No comments:

Post a Comment

Sharing

Get widget