TCS JavaScript Frequently Asked Interview Questions

TCS JavaScript Most Frequently Asked Latest Interview Questions Answers

What does window.print() do in Javascript?

The print() function from window object prints the current web page when executed.

How can a Javascript code redirect the user to a different page?

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.
TCS JavaScript Most Frequently Asked Latest Interview Questions Answers
TCS JavaScript Most Frequently Asked Latest Interview Questions Answers

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().

The following recursive code will cause a stack overflow if the array list is too large. How can you fix this and still retain the recursive pattern?

var list = readHugeList();

var nextListItem = function() {
    var item = list.pop();

    if (item) {
        // process the list item...
        nextListItem();
    }
};

The potential stack overflow can be avoided by modifying the nextListItem function as follows:

var list = readHugeList();

var nextListItem = function() {
    var item = list.pop();

    if (item) {
        // process the list item...
        setTimeout( nextListItem, 0);
    }
};
The stack overflow is eliminated because the event loop handles the recursion, not the call stack. When nextListItem runs, if item is not null, the timeout function (nextListItem) is pushed to the event queue and the function exits, thereby leaving the call stack clear. When the event queue runs its timed-out event, the next item is processed and a timer is set to again invoke nextListItem. Accordingly, the method is processed from start to finish without a direct recursive call, so the call stack remains clear, regardless of the number of iterations.

What is a “closure” in JavaScript? Provide an example.

A closure is an inner function that has access to the variables in the outer (enclosing) function’s scope chain. The closure has access to variables in three scopes; specifically: (1) variable in its own scope, (2) variables in the enclosing function’s scope, and (3) global variables.

Here is an example:

var globalVar = "xyz";

(function outerFunc(outerArg) {
    var outerVar = 'a';

    (function innerFunc(innerArg) {
    var innerVar = 'b';

    console.log(
        "outerArg = " + outerArg + "\n" +
        "innerArg = " + innerArg + "\n" +
        "outerVar = " + outerVar + "\n" +
        "innerVar = " + innerVar + "\n" +
        "globalVar = " + globalVar);

    })(456);
})(123);
In the above example, variables from innerFunc, outerFunc, and the global namespace are all in scope in the innerFunc. The above code will therefore produce the following output:

outerArg = 123
innerArg = 456
outerVar = a
innerVar = b
globalVar = xyz

What will be the output of the following code:

for (var i = 0; i < 5; i++) {
setTimeout(function() { console.log(i); }, i * 1000 );
}
Explain your answer. How could the use of closures help here?

The code sample shown will not display the values 0, 1, 2, 3, and 4 as might be expected; rather, it will display 5, 5, 5, 5, and 5.

The reason for this is that each function executed within the loop will be executed after the entire loop has completed and all will therefore reference the last value stored in i, which was 5.

Closures can be used to prevent this problem by creating a unique scope for each iteration, storing each unique value of the variable within its scope, as follows:

for (var i = 0; i < 5; i++) {
    (function(x) {
        setTimeout(function() { console.log(x); }, x * 1000 );
    })(i);
}
This will produce the presumably desired result of logging 0, 1, 2, 3, and 4 to the console.

In an ES2015 context, you can simply use let instead of var in the original code:

for (let i = 0; i < 5; i++) {
setTimeout(function() { console.log(i); }, i * 1000 );

What would the following lines of code output to the console?

console.log("0 || 1 = "+(0 || 1));
console.log("1 || 2 = "+(1 || 2));
console.log("0 && 1 = "+(0 && 1));
console.log("1 && 2 = "+(1 && 2));
Explain your answer.

The code will output the following four lines:

0 || 1 = 1
1 || 2 = 1
0 && 1 = 0
1 && 2 = 2
In JavaScript, both || and && are logical operators that return the first fully-determined “logical value” when evaluated from left to right.

The or (||) operator. In an expression of the form X||Y, X is first evaluated and interpreted as a boolean value. If this boolean value is true, then true (1) is returned and Y is not evaluated, since the “or” condition has already been satisfied. If this boolean value is “false”, though, we still don’t know if X||Y is true or false until we evaluate Y, and interpret it as a boolean value as well.

Accordingly, 0 || 1 evaluates to true (1), as does 1 || 2.

The and (&&) operator. In an expression of the form X&&Y, X is first evaluated and interpreted as a boolean value. If this boolean value is false, then false (0) is returned and Y is not evaluated, since the “and” condition has already failed. If this boolean value is “true”, though, we still don’t know if X&&Y is true or false until we evaluate Y, and interpret it as a boolean value as well.

However, the interesting thing with the && operator is that when an expression is evaluated as “true”, then the expression itself is returned. This is fine, since it counts as “true” in logical expressions, but also can be used to return that value when you care to do so. This explains why, somewhat surprisingly, 1 && 2 returns 2 (whereas you might it expect it to return true or 1).

How can AJAX applications be debugged?

Two tools are used for debugging:

Fiddler for IE
Firebug for Mozilla.

What is Script Manager?

Script Manager helps manage the client side script of AJAX. Script Manager acts as a mediator as AJAX depends on JavaScript. Every page that uses AJAX has a Script Manager to enable AJAX libraries.

How Ajax objects can be created?

Following syntax can be used to create Ajax objects:

Var sample = New ajaxObject(‘path of the page’)

What are all the different data types that JSON supports?

JSON supports following data types:

String
Number
Boolean
Array
Object
Null

What are the goals of Ajax?

The basic goals of ASP.NET Ajax are:

Reduced web server hits
Reduced Network load
Interactive user interface
Platform and architecture neutrality
Support for both synchronous and asynchronous communication
Provide a server- and client-side framework

What is the difference between proxied and proxyless calls in AJAX?

Proxied calls are made through stub objects which can be called from PHP classes on the JavaScript side in AJAX.

Proxyless calls are made using utility JavaScript functions like HTML_AJAX.replace() and HTML_AJAX.append() in AJAX.

What are the protocols used by Ajax?

HTTP’s GET or POST
XMLHttpRequest for placing a request with the web server
Uses JSON to communicate between the client and server
UED or URL encoded data

What are all the security issues of Ajax?

Security issues that can be encountered

When Ajax calls are sent through plain text and it may lead to know the database details
Inserting scripts can also be possible and attackers can easily penetrate into the system

 How can we handle concurrent requests?

Javascript functions should be written to handle concurrent requests and call back function can be passed as a parameter. Those parameters are passed to AjaxInteraction(URL, callback) object.

Define the role of the Update Panel?

Update Panel is used to add functionality to the existing ASP.NET applications. By using partial page rendering, it can be used to update the content. Refresh can be made for the partial page instead of whole page.

Can we use nested update panel in Ajax?

Yes, we can use nested update panel in Ajax. Update panels can be nested to have more control over the Page Refresh.

What are the types of post back in Ajax?

There are two types of post backs:

Synchronous Postback
Asynchronous Postback

How can we handle exception handling in Ajax?

ErrorTemplate which is the child tag of Script Manager is used to handle exception handling in Ajax.

Post a Comment

Previous Post Next Post