Top 85 Javascript Interview Questions
Top 85 Javascript Interview Questions
com/
JavaScript is a client-side as well as server side scripting language that can be inserted into
HTML pages and is understood by web browsers. JavaScript is also an Object Oriented
Programming language
Java is a complete programming language. In contrast, JavaScript is a coded program that can
be introduced to HTML pages. These two languages are not at all inter-dependent and are
designed for the different intent. Java is an object - oriented programming (OOPS) or structured
programming language like C++ or C whereas JavaScript is a client-side scripting language and
it is said to be unstructured programming.
Number
String
Boolean
Function
Object
Null
Undefined
isNan function returns true if the argument is not a number otherwise it is false.
JavaScript is faster. JavaScript is a client-side language and thus it does not need the
assistance of the web server to execute. On the other hand, ASP is a server-side language and
hence is always slower than JavaScript. Javascript now is also a server side language (nodejs).
1 / 14
http://career.guru99.com/
Negative Infinity is a number in JavaScript which can be derived by dividing negative number by
zero.
Breaking within a string statement can be done by the use of a backslash, ‘\’, at the end of the
first line
Example:
[crayon-582ea38d2782f674565679/]
And if you change to a new line when not within a string statement, then javaScript ignores
break in line.
Example:
[crayon-582ea38d27835486354271/]
The above code is perfectly fine, though not advisable as it hampers debugging.
Undeclared variables are those that do not exist in a program and are not declared. If the
program tries to read the value of an undeclared variable, then a runtime error is encountered.
Undefined variables are those that are declared in the program but have not been given any
value. If the program tries to read the value of an undefined variable, an undefined value is
returned.
Global variables are those that are available throughout the length of the code, that is, these
have no scope. The var keyword is used to declare a local variable or object. If the var keyword
is omitted, a global variable is declared.
Example:
2 / 14
http://career.guru99.com/
The problems that are faced by using global variables are the clash of variable names of local
and global scope. Also, it is difficult to debug and test the code that relies on global variables.
A prompt box is a box which allows the user to enter input by providing a text box. Label and
box will be provided to enter the text or number.
14. Explain the working of timers in JavaScript? Also elucidate the drawbacks of using
the timer, if any?
Timers are used to execute a piece of code at a set time or also to repeat the code in a given
interval of time. This is done by using the functions setTimeout, setInterval and clearInterval.
The setTimeout(function, delay) function is used to start a timer that calls a particular function
after the mentioned delay. The setInterval(function, delay) function is used to repeatedly
execute the given function in the mentioned delay and only halts when cancelled.
The clearInterval(id) function instructs the timer to stop.
Timers are operated within a single thread, and thus events might queue up, waiting to be
executed.
/* Multi
Line
Comment
*/
‘SessionState’ is specific to user specific data that can be accessed across all pages in the
web application.
3 / 14
http://career.guru99.com/
=== is called as strict equality operator which returns true when the two operands are having
the same value without any type conversion.
document.form[0].submit();
Yes JavaScript does support automatic type conversion, it is the common way of type
conversion used by JavaScript developers
There are two ways to read and write a file using JavaScript
For
While
do-while loops
Variable typing is used to assign a number to a variable and the same variable can be assigned
to a string.
Example
[crayon-582ea38d27841810824135/]
This is called variable typing.
24. How can you convert the string of any base to integer in JavaScript?
4 / 14
http://career.guru99.com/
The parseInt() function is used to convert numbers between different bases. parseInt() takes the
string to be converted as its first parameter, and the second parameter is the base of the given
string.
In order to convert 4F (of base 16) to integer, the code used will be -
[crayon-582ea38d27843345727994/]
25. Explain the difference between “==” and “===”?
“==” checks only for equality in value whereas “===” is a stricter equality test and returns false
if either the value or the type of the two variables are different.
Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, its
concatenation will be done. So the result would be 57.
27. Explain how to detect the operating system on the client machine?
In order to detect the operating system on the client machine, the navigator.appVersion string
(property) should be used.
The NULL value is used to represent no value or no object. It implies no object or null string, no
valid boolean value, no number and no array object.
The functionality of delete operator is used to delete all variables and objects in a program but it
cannot delete variables declared with VAR keyword.
31. What are all the types of Pop up boxes available in JavaScript?
Alert
Confirm and
Prompt
5 / 14
http://career.guru99.com/
Void(0) is used to prevent the page from refreshing and parameter "zero" is passed while
calling.
35. What is the difference between an alert box and a confirmation box?
Escape characters (Backslash) is used when working with special characters like single quotes,
double quotes, apostrophes and ampersands. Place backslash before the characters to make it
display.
Example:
[crayon-582ea38d27848244194158/]
37. What are JavaScript Cookies?
Cookies are the small test files stored in a computer and it gets created when the user visits the
websites to store information that they need. Example could be User Name details and
shopping cart information from the previous visits.
The pop() method is similar as the shift() method but the difference is that the Shift method
works at the start of the array. Also the pop() method take the last element off of the given
array and returns it. The array on which is called is then altered.
Example:
var cloths = ["Shirt", "Pant", "TShirt"];
cloths.pop();
//Now cloth becomes Shirt,Pant
No. JavaScript does not have concept level scope. The variable declared inside the function
6 / 14
http://career.guru99.com/
They are as –
Primitive
Reference types.
Primitive types are number and Boolean data types. Reference types are more complex types
like strings and dates.
‘Typeof’ is an operator which is used to return a string description of the type of a variable.
7 / 14
http://career.guru99.com/
Blur function is used to remove the focus from the specified object.
Variable typing is used to assign a number to a variable and then assign string to the same
variable. Example is as follows:
[crayon-582ea38d2784f568009416/]
49. How to find operating system in the client machine using JavaScript?
The ‘Navigator.appversion’ is used to find the name of the operating system in the client
machine.
Load time errors: Errors which come up when loading a web page like improper syntax
errors are known as Load time errors and it generates the errors dynamically.
Run time errors: Errors that come due to misuse of the command inside the HTML
language.
Logical Errors: These are the errors that occur due to the bad logic performed on a
function which is having different operation.
The push method is used to add or append one or more elements to the end of an Array. Using
this method, we can append multiple elements by passing multiple arguments
Unshift method is like push method which works at the beginning of the array. This method is
used to prepend one or more elements to the beginning of the array.
Both are almost similar. JavaScript is developed by Netscape and Jscript was developed by
Microsoft .
8 / 14
http://career.guru99.com/
or
[crayon-582ea38d27854471430837/]
55. What is the ‘Strict’ mode in JavaScript and how can it be enabled?
Strict Mode adds certain compulsions to JavaScript. Under the strict mode, JavaScript shows
errors for a piece of codes, which did not show an error before, but might be problematic and
potentially unsafe. Strict mode also solves some mistakes that hamper the JavaScript engines
to work efficiently.
Strict mode can be enabled by adding the string literal “use strict” above the file. This can be
illustrated by the given example:
[crayon-582ea38d27856272841362/]
56. What is the way to get the status of a CheckBox?
alert(document.getElementById('checkbox1').checked);
The navigator.appVersion string can be used to detect the operating system on the client
machine.
The onload function is not run until all the information on the page is loaded. This leads to a
substantial delay before any code is executed.
onDocumentReady loads the code just after the DOM is loaded. This allows early manipulation
of the code.
59. How will you explain closures in JavaScript? When are they used?
Closure is a locally declared variable related to a function which stays in memory when the
function has returned.
For example:
[crayon-582ea38d27858630213196/]
60. How can a value be appended to an array?
arr[arr.length] = value;
9 / 14
http://career.guru99.com/
A function that is declared without any named identifier is known as an anonymous function. In
general, an anonymous function is inaccessible after its declaration.
The function .call() and .apply() are very similar in their usage except a little difference. .call() is
used when the number of the function’s arguments are known to the programmer, as they have
to be mentioned as arguments in the call statement. On the other hand, .apply() is used when
the number is not known. The function .apply() expects the argument to be an array.
The basic difference between .call() and .apply() is in the way arguments are passed to the
function. Their usage can be illustrated by the given example.
[crayon-582ea38d2785f553175275/]
64. Define event bubbling?
JavaScript allows DOM elements to be nested inside each other. In such a case, if the handler
of the child is clicked, the handler of parent will also work as if it were clicked too.
Yes, JavaScript is case sensitive. For example, a function parseInt is not same as the function
Parseint.
The ‘And’ Operator (&&), ‘Or’ Operator (||) and the ‘Not’ Operator (!) can be used in
JavaScript.
This can be done by including the name of the required frame in the hyperlink using the ‘target’
10 / 14
http://career.guru99.com/
attribute.
[crayon-582ea38d27862620487680/]
68. What is the role of break and continue statements?
Break statement is used to come out of the current loop while the continue statement continues
the current loop with a new recurrence.
Both web-garden and web-farm are web hosting systems. The only difference is that web-
garden is a setup that includes many processors in a single server while web-farm is a larger
setup that uses more than one server.
Assigning properties to objects is done in the same way as a value is assigned to a variable. For
example, a form object’s action value is assigned as ‘submit’ in the following manner -
Document.form.action=”submit”
71. What is the method for reading and writing a file in JavaScript?
This can be done by Using JavaScript extensions (runs from JavaScript Editor), example for
opening of a file -
[crayon-582ea38d27864765605410/]
72. How are DOM utilized in JavaScript?
DOM stands for Document Object Model and is responsible for how various objects in a
document interact with each other. DOM is required for developing web pages, which includes
objects like paragraph, links, etc. These objects can be operated to include actions like add or
delete. DOM is also required to add extra capabilities to a web page. On top of that, the use of
API gives an advantage over other existing models.
Events are the actions that result from activities, such as clicking a link or filling a form, by the
user. An event handler is required to manage proper execution of all these events. Event
handlers are an extra attribute of the object. This attribute includes event’s name and the action
taken if the event takes place.
By default, the parsing of the HTML code, during page loading, is paused until the script has not
stopped executing. It means, if the server is slow or the script is particularly heavy, then the
webpage is displayed with a delay. While using Deferred, scripts delays execution of the script
till the time HTML parser is running. This reduces the loading time of web pages and they get
displayed faster.
11 / 14
http://career.guru99.com/
First-class functions: Functions in JavaScript are utilized as first class objects. This usually
means that these functions can be passed as arguments to other functions, returned as values
from other functions, assigned to variables or can also be stored in data structures.
Nested functions: The functions, which are defined inside other functions, are called Nested
functions. They are called ‘everytime’ the main function is invoked.
Load-time errors: The errors shown at the time of the page loading are counted under
Load-time errors. These errors are encountered by the use of improper syntax, and thus
are detected while the page is getting loaded.
Run-time errors: This is the error that comes up while the program is running. It is
caused by illegal operations, for example, division of a number by zero, or trying to
access a non-existent area of the memory.
Logic errors: It is caused by the use of syntactically correct code, which does not fulfill
the required task. For example, an infinite loop.
Screen objects are used to read the information from the client’s screen. The properties of
screen objects are -
This method is functional at the starting of the array, unlike the push(). It adds the desired
number of elements to the top of an array. For example -
[crayon-582ea38d27868101460297/]
The output is shown below:
[crayon-582ea38d2786a597310277/]
79. Define unescape() and escape() functions?
The escape () function is responsible for coding a string so as to make the transfer of the
information from one computer to the other, across a network.
12 / 14
http://career.guru99.com/
For Example:
[crayon-582ea38d2786c217267971/]
Output: Hello%3F%20How%20are%20you%21
EncodeURl() is used to convert URL into their hex coding. And DecodeURI() is used to convert
the encoded URL back to normal.
[crayon-582ea38d27870693132520/]
Output -
my%20test.asp?name=st%C3%A5le&car=saab
my test.asp?name=ståle&car=saab
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.
ECMA Script are like rules and guideline while Javascript is a scripting language used for web
development.
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.
85. How can JavaScript codes be hidden from old browsers that don’t support
JavaScript?
13 / 14
http://career.guru99.com/
Add “” without the quotes in the code just before the tag.
Old browsers will now treat this JavaScript code as a long HTML comment. While, a browser
that supports JavaScript, will take the “” as one-line comments.
14 / 14
Powered by TCPDF (www.tcpdf.org)