Interview Question & Answers for Javascript
1. What is JavaScript?
JavaScript is a client-side and server-side scripting language inserted into
HTML pages and is understood by web browsers. JavaScript is also an Object-
based Programming language
2. Enumerate the differences between Java and JavaScript?
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 different intent. Java is an object-
oriented programming (OOPS) or structured programming languages like C++
or C, whereas JavaScript is a client-side scripting language.
3. What are JavaScript Data Types?
Following are the JavaScript Data types:
Number
String
Boolean
Object
Undefined
4. Which is faster between JavaScript and an ASP script?
JavaScript is faster. JavaScript is a client-side language,, and thus it does not
need the assistance of the webserver 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).
5. Is it possible to break JavaScript Code into several lines?
Breaking within a string statement can be done by using a backslash, ‘\,’ at the
end of the first line.
Example:
document. Write ("This is \a program,");
And if you change to a new line when not within a string statement, then
javaScript ignores the break in the line.
Example:
var x=1, y=2,
z=
x+y;
The above code is perfectly fine, though not advisable as it hampers
debugging.
6. Which company developed JavaScript?
Netscape is the software company that developed JavaScript.
7. What are undeclared and undefined variables?
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.
8. What are global variables? How are these variable declared?
Global variables are available throughout the length of the code so that it has
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:
// Declare a global: globalVariable = “Test”;
The problems 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.
9. What is a prompt box?
A prompt box is a box that allows the user to enter input by providing a text
box. A label and box will be provided to enter the text or number.
10. What is ‘this’ keyword in JavaScript?
‘This’ keyword refers to the object from where it was called.
11. What is the working of timers in JavaScript?
Timers are used to execute a piece of code at a set time or repeat the code in a
given interval. 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 repeatedly executes the given function in the mentioned delay
and only halts when canceled. 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.
12. Which symbol is used for comments in Javascript?
// for Single line comments and
/* Multi
Line
Comment
*/
13. What is === operator?
=== is called a strict equality operator, which returns true when the two
operands have the same value without conversion.
14. How you can submit a form using JavaScript?
To submit a form using JavaScript use
document.form[0].submit();
document.form[0].submit();
15. Does JavaScript support automatic type conversion?
Yes, JavaScript does support automatic type conversion. It is the common way
of type conversion used by JavaScript developers
16. How can the style/class of an element be changed?
It can be done in the following way:
document.getElementById("myText"). style. fontSize = "20";
or
document. getElementById ("myText"). className = "anyclass";
17. What are all the looping structures in JavaScript?
Following are looping structures in Javascript:
For
While
Do-while loops
18. What is called Variable typing in Javascript?
Variable typing is used to assign a number to a variable. The same variable can
be assigned to a string.
Example:
i = 10;
i = "string;"
This is called variable typing.
19. 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.
20. 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.
21. What would be the result of 3+2+”7″?
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.
22. What do you mean by NULL in Javascript?
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.
23. What is the function of the delete operator?
The delete keyword is used to delete the property as well as its value.
Example
var student= {age:20, batch:"ABC"};
Delete student. age;
24. What is an undefined value in JavaScript?
Undefined value means the
Variable used in the code doesn’t exist
Variable is not assigned to any value
Property does not exist.
25. What are all the types of Pop up boxes available in JavaScript?
Alert
Confirm and
Prompt
26. How can a page be forced to load another page in JavaScript?
The following code has to be inserted to achieve the desired effect:
<script language="JavaScript" type="text/javascript" >
<!-- location. href="https://www.guru99.com/javascript-interview-
questions-answers.html"; //--></script>
27. What is the data type of variables in JavaScript?
All variables in JavaScript are object data types.
28. What is the difference between an alert box and a confirmation box?
An alert box displays only one button, which is the OK button.
But a Confirmation box displays two buttons, namely OK and cancel.
29. What are escape characters?
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:
document. write "I m a "good" boy."
document. write "I m a \"good\" boy."
30. What are JavaScript Cookies?
Cookies are the small test files stored in a computer, and they get
created when the user visits the websites to store information that they
need. Examples could be User Name details and shopping cart
information from previous visits.
31. What a pop()method in JavaScript is?
The pop() method is similar to the shift() method, but the difference is
that the Shift method works at the array’s start. The pop() method takes
the last element off of the given array and returns it. The array on which
it is called is then altered.
Example:
var cloths = ["Shirt", "Pant", "TShirt"];
cloths.pop();
//Now cloth becomes Shirt,Pant
32. What are the disadvantages of using inner HTML in JavaScript?
If you use innerHTML in JavaScript, the disadvantage is
Content is replaced everywhere
We cannot use it like “appending to innerHTML
Even if you use +=like “innerHTML = innerHTML + ‘html'” still the old
content is replaced by html
The entire innerHTML content is re-parsed and builds into elements.
Therefore, it’s much slower
The innerHTML does not provide validation, and therefore we can
potentially insert valid and broken HTML in the document and break it
33. What is break and continue statements?
Break statement exits from the current loop.
Continue statement continues with next statement of the loop.
34. What are the two basic groups of data types in JavaScript?
They are as—Primitive
Reference types
Primitive types are number and Boolean data types. Reference types are more
complex types like strings and dates.