June 3, 2019

Srikaanth

Cyient JavaScript Interview Questions Answers

Cyient JavaScript Most Frequently Asked Latest Interview Questions Answers

What is the use of the ‘this’ keyword?

The keyword ‘this’ refers to the current instance of the object when used inside a function. But, when used outside a function, it refers to the window object.

Is Exception handling possible in Javascript?

With the latest version of Javascript, exception handling is possible; and this can be achieved using the following keywords try, catch and finally.

How is it possible to get the total number of arguments that are passed to a function?

The arguments.length property helps in getting the total number of arguments that are passed to a function.

What is the difference between typeof and instanceof operators in Javascript?

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

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.

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.

What is the difference between textContent and innerText?

Let us have a paragraph element and a span element in it as a child element.
<p>some text and a <span style="visibility: hidden">span tag hidden <\span>in it</p>
Now, if the following two steps would result in the following-
console.log(document.querySelector('p').textContent); gives some text and a span tag hidden in it.
console.log(document.querySelector('p').innerText); gives some text and a in it.

What is the difference between HTMLCollection and NodeList?

The functions querySelectorAll() returns NodeList in which the forEach can be used directly to traverse the elements. Whereas, the getElementsByClassName() or getElementsByTagName() returns an HTMLCollection, which does not have a forEach by default.

What will be the output when the following code is executed? Explain.

console.log(false == '0')
console.log(false === '0')

The code will output:

true
false
In JavaScript, there are two sets of equality operators. The triple-equal operator === behaves like any traditional equality operator would: evaluates to true if the two expressions on either of its sides have the same type and the same value. The double-equal operator, however, tries to coerce the values before comparing them. It is therefore generally good practice to use the === rather than ==. The same holds true for !== vs !=.

What is the output out of the following code? Explain your answer.

var a={},
    b={key:'b'},
    c={key:'c'};

a[b]=123;
a[c]=456;

console.log(a[b]);

The output of this code will be 456 (not 123).

The reason for this is as follows: When setting an object property, JavaScript will implicitly stringify the parameter value. In this case, since b and c are both objects, they will both be converted to "[object Object]". As a result, a[b] anda[c] are both equivalent to a["[object Object]"] and can be used interchangeably. Therefore, setting or referencing a[c] is precisely the same as setting or referencing a[b].

What will the following code output to the console:

console.log((function f(n){return ((n > 1) ? n * f(n-1) : n)})(10));
Explain your answer.

The code will output the value of 10 factorial (i.e., 10!, or 3,628,800).

Here’s why:

The named function f() calls itself recursively, until it gets down to calling f(1) which simply returns 1. Here, therefore, is what this does:

f(1): returns n, which is 1
f(2): returns 2 * f(1), which is 2
f(3): returns 3 * f(2), which is 6
f(4): returns 4 * f(3), which is 24
f(5): returns 5 * f(4), which is 120
f(6): returns 6 * f(5), which is 720
f(7): returns 7 * f(6), which is 5040
f(8): returns 8 * f(7), which is 40320
f(9): returns 9 * f(8), which is 362880
f(10): returns 10 * f(9), which is 3628800

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
   }
}

What role of #&& in querystring?

# is treated as fragment delimiter to delimit the history state and && precedes is used to check on the information in the query string.

How to control the duration of an Ajax request?

AsyncPostBackTimeout property is used to control the duration of Ajax request. Deafult value of this property is 90 seconds.

Example –

<asp:ScriptManager runat=”server” id=”sample” AsyncPostBackTimeout=”40”/>
1
<asp:ScriptManager runat=”server” id=”sample” AsyncPostBackTimeout=”40”/>

What are the advantages of Ajax?

Following are the advantages of Ajax:

Bandwidth utilization – It saves memory when the data is fetched from the same page.
More interactive
Speeder retrieval of data

What is JSON in Ajax?

JSON is abbreviated as JavaScript Object Notation.

JSON is a safe and reliable data interchange format in JavaScript, which is easy to understand for both users and machines.

What is AJAX Control Extender Toolkit?

AJAX Control Toolkit is one of the extenders that are used to extend or add the functionalities of the ASP.NET controls. The extenders use a block of JavaScript code to add new and enhanced capabilities to the ASP.NET controls.

AJAX Control Extender Toolkit is a free download from site.

Where AJAX cannot be used?

Users cannot use AJAX if

If Page need to show in a search engine
If browser does not support JavaScript
If user wants to create secure application

What are the difference between AJAX and Javascript?

The differences between AJAX and JavaScript are as follows:

AJAX

Javascript

AJAX sends request to the server and does not wait for the response. It performs other operations on the page during that time JavaScript make a request to the server and waits for response
AJAX does not require the page to refresh for downloading the whole page JavaScript manages and controls a Web page after being downloaded
AJAX minimizes the overload on the server since the script needs to request once JavaScript posts a request that updates the script every time.

https://mytecbooks.blogspot.com/2019/06/cyient-javascript-interview-questions.html
Subscribe to get more Posts :