The typeof operator returns a string of what type the operand is. Whereas, the instanceof operator does not work with primitive data types; but works with objects and checks on what type the object is.
What is DOM?
DOM (Document Object Model) is an object-oriented representation of the HTML elements. All the elements (or nodes) are part of window.document.
Expand BOM and explain it.
BOM stands for Browser Object Model. Using BOM interaction with a browser is possible. Default object of the browser is a window.
What is the difference between window and document in Javascript?
Javascript window is a global object which holds variables, functions, history, location; the document also comes under the window and can be considered as the property of the window.
Hitachi Most Frequently Asked Latest JavaScript Interview Questions Answers |
What is the difference between innerHTML and innerText?
innerHTML will process an HTML tag if found in a string, whereas innerText will not. For Example document.querySelector('p').innerHTML='one <br> two' gives the output one and two in two lines as <br> in html is a new line. Whereas document.querySelector('p').innerText='one <br> two' gives the output of the text as it is in one line.
Consider the code snippet below. What will the console output be and why?
(function(x) {
return (function(y) {
console.log(x);
})(2)
})(1);
The output will be 1, even though the value of x is never set in the inner function. Here’s why:
As explained in our JavaScript Hiring Guide, a closure is a function, along with all variables or functions that were in-scope at the time that the closure was created. In JavaScript, a closure is implemented as an “inner function”; i.e., a function defined within the body of another function. An important feature of closures is that an inner function still has access to the outer function’s variables.
Therefore, in this example, since x is not defined in the inner function, the scope of the outer function is searched for a defined variable x, which is found to have a value of 1.
What will the following code output to the console and why:
var hero = {
_name: 'John Doe',
getSecretIdentity: function (){
return this._name;
}
};
var stoleSecretIdentity = hero.getSecretIdentity;
console.log(stoleSecretIdentity());
console.log(hero.getSecretIdentity());
What is the issue with this code and how can it be fixed.
The code will output:
undefined
John Doe
The first console.log prints undefined because we are extracting the method from the hero object, so stoleSecretIdentity() is being invoked in the global context (i.e., the window object) where the _name property does not exist.
One way to fix the stoleSecretIdentity() function is as follows:
var stoleSecretIdentity = hero.getSecretIdentity.bind(hero);
Create a function that, given a DOM Element on the page, will visit the element itself and all of its descendents (not just its immediate children). For each element visited, the function should pass that element to a provided callback function.
The arguments to the function should be:
a DOM element
a callback function (that takes a DOM element as its argument)
Visiting all elements in a tree (DOM) is a classic Depth-First-Search algorithm application. Here’s an example solution:
function Traverse(p_element,p_callback) {
p_callback(p_element);
var list = p_element.children;
for (var i = 0; i < list.length; i++) {
Traverse(list[i],p_callback); // recursive call
}
}
The window.location is assigned a value; i.e., a web link. On its execution, the Javascript code can redirect the user to the mentioned web link.
What are Cookies in Javascript?
A Cookie is a variable that is stored on a client's/visitors machine. Using Cookies, the servers can identify the client and the client's transactions.
How do Javascript primitive/object types passed in functions?
Primitive types in Javascript are passed by value; whereas, object types are passed by reference.
What is NaN in Javascript?
NaN is a short form of Not a Number. When a string or something else is being converted into a number and that cannot be done, then we get to see NaN. A strange thing about NaN is that it is not equal to anything including itself.
List out all the falsifying tokens in Javascript.
There are 6 tokens that falsify in Javascript and they are false, null, undefined, 0, NaN.
What is Currying in Javascript?
A partial invocation of a Javascript function is called Currying. Few arguments of a function are processed and a function is returned. Few more arguments are added by the returning function.
When do we use JSON.stringify()?
The JSON.stringify() method is used to convert a Javascript data to a string.
What does unshift() function do in Javascript?
Just like push() which inserts elements into an array at the end of it, the unshift() function inserts elements at the beginning of an array.
Why jQuery is needed?
jQuery is needed for the following list:
Used to develop browser compatible web applications
Improve the performance of an application
Very fast and extensible
UI related functions are written in minimal lines of codes
Whether jQuery HTML work for both HTML and XML documents?
No, jQuery HTML only works for HTML documents not for XML Documents.
What are the methods used to provide effects?
Some of the effects methods are:
Show()
Hide()
Toggle()
FadeIn() and
FadeOut()
What is the advantage of using minimized version of jQuery?
Efficiency of web page increases when minimized version of jQuery is used.min.js file will be more than 50% less than the normal js file. Reduction in the file size makes the web page faster.
Is jQuery is a JavaScript or JSON library file?
jQuery is a library of JavaScript file and it consists of DOM, event effects and the Ajax functions. jQuery is said to be a single JavaScript file.
Which operating system is more compatible with jQuery?
Mac, Windows and Linux are more compatible with the jQuery.
How can we include jQuery library in ASP.Net project?
Download the jQuery library from jQuery.com and include that reference in the asp.net page.
Which command will give a version of jQuery?
The command $.ui.version returns jQuery UI version.
In what scenarios jQuery can be used?
jQuery can be used in following scenarios:
Apply CSS static or dynamic
Calling functions on events
Manipulation purpose
Mainly for Animation effects
What is the difference between find and children methods?
Find method is used to find all levels down the DOM tree but children find single level down the DOM tree.
Post a Comment