Javascript Interview Questions and Answers – Part13

121. Why it is not advised to use innerHTML in JavaScript?
innerHTML content is refreshed every time and thus is slower. There is no scope for validation in innerHTML and, therefore, it is easier to insert rouge code in the document and, thus, make the web page unstable.

122. What does the following statement declares?
var myArray = [[[]]];
It declares a three dimensional array.

123. How are JavaScript and ECMA Script related?
ECMAScript is nothing but another name for JavaScript. Precisely, ECMAScript is the formal name of JavaScript, when XML elements have to be accessed.

124. What is namespacing in JavaScript and how is it used?
Namespacing is used for grouping the desired functions, variables etc. under a unique name. It is a name that has been attached to the desired functions, objects and properties. This improves modularity in the coding and enables code reuse.

125. How can JavaScript codes be hidden from old browsers that don’t support JavaScript?
For hiding JavaScript codes from old browsers:
Add “<!–” without the quotes in the code just after the <script> tag.
Add “//–>” without the quotes in the code just before the <script> tag.
Old browsers will now treat this JavaScript code as a long HTML comment. While, a browser that supports JavaScript, will take the “<!–” and “//–>” as one-line comments.

126. Does JavaScript support foreach loop?
JavaScript 1.6(ECMAScript 5th Edition) support foreach loop,

127. What looping structures are there in JavaScript?
for, while, do-while loops

128. 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
var man = new Object(); = ‘Vikas Ahlawat’; = true;
man.age = 27;

129. How you will add function as a property in a JavaScript object? Give an example.
var man = new Object(); = ‘Vikas Ahlawat’; = true;
man.age = 27;
man.getName = function() { return;}
console.log(man.getName()); // Logs ‘Vikas Ahlawat’.

130. 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.
Both will call String() constructor function
You can confirm it by running the following statement:
console.log(myString.constructor, myStringLiteral.constructor);

Sign-up for our email newsletter and get free job alerts, current affairs and GK updates regularly.
Subscribe Here (or)

Leave a Reply

Your email address will not be published. Required fields are marked *

five × 8 =

Popup Dialog Box Powered By :
  • RSS
  • Facebook
  • Google+
  • Twitter