12 Sept 2014

10 Always Asked JavaScript Interview Questions and Answers

Q1: How can we submit a form without a submit button? Or how can we submit a form using JavaScript?

Ans: A form can not only be submitted from html code but also from JavaScript code. You need to know the form name at least.Submit() function is used to submit the form instead of submit button.
E.g.
<form id="myForm" action="form_submitted.php">
<a href="javascript: submitMyForm()">Search</a>
</form>
<script>
function submitMyForm() {
document.getElementById("myForm").submit();
/* you can also use below DOM technique, but for that form tag should contain name of the form like this:
<form name=”myForm1”> */
document.forms["myForm1"].submit(); // or
document.myForm1.submit();
}
</script>


Q2: How to check that a variable is a number?

Ans: There is predefined function to check if a variable is a number or not.
isNaN: returns true if the argument is not a number. NAN itself means Not aNumber.
E.g.    isNaN(“hello”) // returns true.
isNaN(145) // returns false.

Q3: How we can create arrays in JavaScript?

Ans: There are two ways of creating arrays in JavaScript. Using,
1. Array literal
2. New keyword
Array literal Syntax:
var cars = [“BMW”, “AUDI”, “MERCEDES”, “HYUNDAI”];
New Keyword syntax:
var cars = new Array(“BMW”, “AUDI”, “MERCEDES”, “HYUNDAI”);
Both of them work exactly the same way, but it’s better to use first one as it is simple, speedy & easily readable.

Additional Information:

Accessing the values from an array, var carName = cars[0];
Modifying the first element, cars[0] = “VOLKSWAGEN”;
Note: Type of Array is object, so arrays are objects in JavaScript.

Q4: What is the data type of variables in JavaScript?

Ans: In JavaScript there are 7 types of data types – 3 primary, 2 composite and 2 special.
Primary(Primitive) – String, Number and Boolean.
Composite(Reference) – Object and Array.
Special – Null and Undefined.
Javascript has dynamic data types, means same variable can be used as different types.
E.g.    
var a;                   // a is undefined
var a = 55;          // a is a Number
var a = “Hello”;  // a is a String

Q5: Are JavaScript Scripts faster than server side scripts?

Ans: Yes, JavaScript is faster than any server side script because it’s written for client side browsers or machines, so it doesn't need web server’s support for execution.

Q6: How JavaScript code can be inserted into a webpage?

Ans: JavaScript can be inserted anywhere in the page – either <body> or in the <head> section, but it should be enclosed within <script></script> tags.
<script>
// some code here…
</script>
You might see in some old codes, ‘type=”text/javascript”’ in the script tag. It is no longer required as JavaScript is the default scripting languages these days in all major browsers.
<script type=”text/javascript”>
// some code here…
</script>
Note: Please remember external scripts shouldn't contain <SCRIPT> tags.

Q7: How to access external JavaScript files?

Ans: To access the external JavaScript file/files you need to add a reference of it in your code. The reference is provided by src attribute.
Syntax:
<script type="text/javascript" src="myscript.js"></script>
Here myscript.js is the external js file to be included.

Q8: What is the sum of this?

a. “1”+2+3
b. 2+5+”5”
c. 3+”7”+1
Ans: There is a simple rule in JavaScript – if you add a number and string, the result will be a string.
So,
“1”+2+3 = 123
2+5+”5” = 75
3+”7”+1 = 371
Remember operations are performed left to right. So if first one is a string, it considers everything after it as a string, so “1”+2+3 equals to 123 rather than 15.

Q9: How to resolve the JavaScript code rendering issue in old browsers? Or How to hide JavaScript code from old browsers that don’t support JavaScript?

Ans: This question is not much relevant these days as almost all browsers these days are quite advanced and support JavaScript. Still you should know about it. One can comment whole JavaScript code inside the SCRIPT tags.
Syntax:
<script type="text/javascript" language="JavaScript">
<!--
Put your JavaScript code here.
//-->
</script>
Old browsers will interpret JS code as an HTML comment.

Another way is using NOSCRIPT tag. It can be used both in HTML4 & 5 but not supported in XHTML.
The <noscript> tag defines an alternate content for users that have disabled scripts in their browser or have a browser that doesn't support script.
Syntax:
<script>
document.write("Hello")
</script>
<noscript>Sorry, your browser does not support JavaScript!</noscript>
In HTML 4, NOSCRIPT tag can be used only inside <body> element but in HTML 5 it can be used both inside <head> and <body>.

Q10: How to create new objects in JavaScript and assign object properties?

Ans: It’s very easy to create new objects in JavaScript.NEW keyword is used to create objects.
Syntax: varObj = new object(); or varObj = {}

You can also create your own objects and add properties to it.
E.g. var car = {name:”Audi”, model:”A6”, color:”black”, cc:”2500”}

Object properties can be assigned like this.
                Obj[“age”] = 22or car.model = “R8”;



0 comments:

Post a Comment