Top 10 JavaScript Interview Question Answers

Javascript Interview Questions and Answers.

Javascript is a very popular programming language used to create interactive websites. Javascript is the default scripting language of the browser. Also, much test automation is written in Javascript, so let us get started.

In Javascript, what happens when two functions call each other recursively?

The recursive calls stop after some time automatically. What is the result of applying an arithmetic operator on a string? The result is NaN, meaning Not a Number. It is given as a result when a string cannot be converted to a number. How can you reduce the time it takes Javascript switch statement to execute? Write a break statement after every case. A break statement ensures that the remaining cases are not tested again.

What is the difference between == and === operators in Javascript?

== compares only the value but === compares both the value and the data type. What happens when a Javascript function is called with extra arguments, for example the function is defined with two parameters, but is called with three arguments? All the extra arguments at the end are ignored and the function executes.

Which Javascript loop runs at least once?

It is a do-while loop because do-while loop tests the condition for the first time after running the body of the loop.

What is the result of running this loop where i = 1 for statement if i > 5 break; document.write (++i); Now, the for loop has no expression, so the loop has to break in the body of the loop. The loop starts with the value of i as 1 but it writes i after incrementing it. So, the first time it writes 2. Then i is incremented by 1 every time. Finally, the loop prints 6 and 6 > 5 so it breaks. So, the answer is it writes 23456.

In Javascript, what is the preferred way of creating just one instance of a user-defined object?

Object initializer, this is the shorthand for creating an object using just one line of code. The concat method does what in Javascript? concat creates a new array joining two or more existing arrays.

In Javascript, the elements of an associative array can be referred by their number index. True or False?

The answer is false. The elements of an associative array can be referred only by their string index, not by their number index. Where can Javascript code be placed within an HTML document? In the HEAD section or in the BODY section, but functions are a part of the HEAD section as a common practice for more readability. What does the expression… this expression return? Now, the innermost Math.random returns a number between 0 and 1. The outer Math.round rounds it off to 0 decimal places, so it returns either a 0 or a 1. Which of the following date object methods has a 1-based return value? That is, it returns the number between 1 and something? getMinutes, getHours, getDay or getDate? The answer is getDate, because it returns the number between 1 and 31 depending on the date value.

Which function returns a part of an array without affecting it?

slice

How can parameters be accessed within the function body for a function defined without parameters, but called with arguments?

By using the arguments object. Now, arguments is an object that contains each parameter passed in the function call.

Which function can be invoked on an existing node in DOM to add a child node? appendChild, addChild, includeChild or setChild?

The answer is appendChild. What is the correct syntax for opening a new window or tab in the browser? openWindow, document.window or window.open? It is window.open Also, window.open function has a large number of options.

Which data should be set in document.cookie to create a cookie? cookie name, cookie value, expiration date or all of these? The answer is all of these. Not setting the expiration date deletes the cookie as soon as the browser is closed.

The try statement requires at least which one of the following? a throw statement, a catch statement, a finally statement or either a catch statement or a finally statement? Either a catch statement or a finally statement. With Javascript object represents the browser? document object, screen object, window object or navigator object? The answer is navigator object. The navigator object has properties like appName meaning browser name and appVersion meaning browser version. That’s all in this video. I hope you found it useful. Thank you and see you next time.