JavaScript Int Qs
JavaScript Int Qs
JavaScript Int Qs
com
1) What is JavaScript?
Ans:JavaScript is a scripting language most often used for client-side web development.
2) What is the difference between JavaScript and Jscript?
Ans:Both JavaScript and Jscript are almost similar. JavaScript was developed by
Netscape. Microsoft reverse engineered Javascript and called it JScript.
3) 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
If your script code is very short and only for single page, then following ways are the
best:
a) You can place <script type="text/javascript"> tag inside the <head> element.
Code
Collapse | Copy 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
Collapse | Copy Code
<head>
<title>Page Title</title>
<script type="text/javascript" src="myjavascript.js"></script>
</head>
<!DOCTYPE html>
<html>
<body>
Full name: <input type="text" id="txtFullName"
name="FirstName" value="Vikas Ahlawat">
</body>
</html>
There are following ways to access the value of the above textbox:
Collapse | Copy Code
or:
we can use the old way:
Collapse | Copy Code
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.
9) What are the ways of making comments in JavaScript?
Ans:
Collapse | Copy Code
10) How will you get the Checkbox status whether it is checked or not?
Ans:
Collapse | Copy Code
12) If an array with name as "names" contain three elements, then how will
you print the third element of this array?
Ans: Print third array element document.write(names[2]);
Note:- Array index starts with 0.
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.
Example:
Code
Collapse | Copy Code
document.write(isNaN("Hello")+ "<br>");
document.write(isNaN("2013/06/23")+ "<br>");
document.write(isNaN(123)+ "<br>");
true
true
false
document.getElementById(myText).style.fontSize = 10";
document.getElementById(myText).className = anyclass;
23) How you will add function as a property in a JavaScript object? Give an
example.
Ans:
Code
Collapse | Copy Code
24) What is the similarity between the 1st and 2nd statement?
1st:- var myString = new String('male'); // An object.
2nd:- var myStringLiteral = 'male'; // Primitive string value, not an object.
Ans: Both will call String() constructor function
You can confirm it by running the following statement:
Collapse | Copy Code
console.log(myString.constructor, myStringLiteral.constructor);
26) Consider the following statements and tell what would be the output of
the logs statements?
Collapse | Copy Code
Ans:
Collapse | Copy Code
Ans: // Logs false, JavaScript 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.
28) What would be the output of the following statements?
Code
Collapse | Copy Code
30) Name any two JavaScript functions which are used to convert nonnumeric
values into numbers?
Ans:
Collapse | Copy Code
Number()
parseInt()
parseFloat()
Code
Collapse | Copy Code
var
var
var
var
var
n1
n2
n3
n4
n5
=
=
=
=
=
31) Does JavaScript Support automatic type conversion, If yes give example.
Ans: 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.
Collapse | Copy Code
var s = '5';
var a = s*1;
var b = +s;
typeof(s); //"string"
typeof(a); //"number"
typeof(b); //"number"
http://dglobaltech.com
(1) What is event bubbling and capturing in JavaScript?
Event bubbling is series of events when : An event is triggered on the inner most
element first and then it triggers on the parent elements in nested order.
DOM elements could be nested inside each other. The handler of the parent works
even if you click on its child. Event bubbling can make this happen.
Event Capturing is the opposite of bubbling where the event is triggered on the
outer most element and then it triggers the inner children in nested order.
(2) What is a closure in JavaScript?
JavaScript allows you to declare a function within another function, and the local
variables still can be accessible even after returning from the function you called.
In other words, a closure is the local variables for a function which is kept alive after
the function has returned.
An example :
function goodMorning(name) {
var text = Good Morning + name; // local variable
var goodMorningAlert = function() { alert(text); }
return sayAlert;
}
var goodMorningAlert2 = goodMorning(Bob);
goodMorningAlert2();
In the example above, when goodMorningAlert2() will be executed, you will see
output as an alert with Good Morning Bob.
In short, in JavaScript, if you use the function keyword inside another function, you
are creating a closure.
(3) What is a callback function in JavaScript?
(4) What are various methods youd use to embed JavaScript onto a webpage?
(5) What is the difference between GET and POST methods in HTML form?
When you use a GET method, key and values will be appended at the end of the
URL as a query string. GET method is not recommended to use when you are
passing sensitive data over the internet.
While sending data, the GET method appends the data to the URL. And the length of
a URL is limited. Maximum URL length is 2048 characters supported by latest
browsers.
POST is used to pass sensitive data as its not appended and displayed within the
URL. There is no limitation as to how much data can be passed using a POST
method.
(6) What does continue and break statements do in JS?
The continue statement breaks only one iteration in the current loop if a defined
condition occurs. and it continues with the next iteration in the loop.
The break statement breaks out of the loop. And it lets the code continue executing
after the loop.
(7) What is the difference between windows.onload and
onDocumentReady?
The onload event does not fire until the last element of the page is loaded including
CSS and images etc. There could be a huge delay before onload code on the page is
executed.
onDocumentReady will be able to execute code as long as DOM has been is built;
without waiting for the elements to be loaded, such as images. That means there
will be a faster code execution.
(8) what is a race condition?
When execution of a certain method or an event call is critically dependent on an
another method or event; and if the sequence is not maintained, race condition
occurs. As an example; youre loading an external Javascript on your page and then
calling a function from that external JS. Now at the run time if the external JS is not
loaded for any reason and your function is already called and looking for that code
execution, its not going to be executed. This situation is called race condition.
(9) What is JS namespacing? How and where is it used?
adding answer soon
(10) Tell me datatypes that are supported in JavaScript.
JavaScript supports various data types such as numbers, strings, arrays, objects
etc. You can also directly assign expressions. Such as :
var datapoint = a * 10; // Number assigned by an expression literal