Are you preparing for a JavaScript interview? You’ve come to the right place. Interviews can be nerve-wracking, regardless of your level of expertise or experience. From choosing the right attire to anticipating the questions you’ll face, there’s a lot to consider.
But fret not! We’re here to lend a helping hand. In this article, we’ve compiled a comprehensive list of common JavaScript interview questions that every candidate should be familiar with. Take some time to review these questions and test your knowledge. If you can answer them confidently, you’re on the right track and likely to ace the interview. If not, don’t worry—it’s an opportunity to enhance your skills and better prepare yourself.
So dive into the following JavaScript interview questions, and equip yourself for success in your upcoming interview.
Contents
Also check- Core java interview questions / Jquery interview questions
JavaScript interview questions
Q1.What is JavaScript?
Ans-
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 based Programming languageQ2.Enumerate the differences between Java and JavaScript?
Ans-
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.Q3.What are JavaScript Data Types?
Ans-
Following are the JavaScript Data types:
–
Number
String
Boolean
Object
UndefinedQ4.What is the use of isNaN function?
Ans-
isNan function returns true if the argument is not a number otherwise it is false.Q5.Between JavaScript and an ASP script, which is faster?
Ans-
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).Q6.Which company developed JavaScript?
Ans-
Netscape is the software company who developed JavaScript.Q7.What is the difference between ViewState and SessionState?
Ans-
‘ViewState’ is specific to a page in a session.
–
‘SessionState’ is specific to user specific data that can be accessed across all pages in the web application.Q8.What is === operator?
Ans-
=== is called as strict equality operator which returns true when the two operands are having the same value without any type conversion.Q9.Explain how can you submit a form using JavaScript?
Ans-
To submit a form using JavaScript use document.form[0].submit();
document.form[0].submit();Q10.Does JavaScript support automatic type conversion?
Ans-
Yes JavaScript does support automatic type conversion, it is the common way of type conversion used by JavaScript developersQ11.How can the style/class of an element be changed?
Ans-
It can be done in the following way:
document.getElementById(“myText”).style.fontSize = “20?;
or
document.getElementById(“myText”).className = “anyclass”;Q12.Explain how to read and write a file using JavaScript?
Ans-
There are two ways to read and write a file using JavaScript
–
Using JavaScript extensions
Using a web page and Active X objectsQ13.What are all the looping structures in JavaScript?
Ans-
Following are looping structures in Javascript:
For
While
do-while loopsQ14.What are undeclared and undefined variables?
Ans-
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.Q15.What is negative infinity?
Ans-
Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.Related: Java 8 Interview / Java Collections Interview
JavaScript interview questions and answers
Q16.Write the code for adding new elements dynamically?
Ans-
<html>
<head>
<title>t1</title>
<script type=”text/javascript”>
function addNode() { var newP = document.createElement(“p”);
var textNode = document.createTextNode(” This is a new text node”);
newP.appendChild(textNode); document.getElementById(“firstP”).appendChild(newP); }
</script> </head>
<body> <p id=”firstP”>firstP<p> </body>
</html>Q17.What are global variables? How are these variable declared and what are the problems associated with using them?
Ans-
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:
// Declare a global globalVariable = “Test”;
–
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.Q18.What is a prompt box?
Ans-
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.Q19.What is ‘this’ keyword in JavaScript?
Ans-
‘This’ keyword refers to the object from where it was called.Q20.Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any?
Ans-
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.Q21.Which symbol is used for comments in Javascript?
Ans-
// for Single line comments and
/* Multi
Line
Comment
*/Q22.Explain the difference between “==” and “===”?
Ans-
“==” 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.Q23.What would be the result of 3+2+”7″?
Ans-
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.Q24.Explain how to detect the operating system on the client machine?
Ans-
In order to detect the operating system on the client machine, the navigator.platform string (property) should be used.Q25.What do mean by NULL in Javascript?
Ans-
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.
Advanced JavaScript interview questions
Q26.What is the function of delete operator?
Ans-
The delete keyword is used to delete the property as well as its value.
Example
var student= {age:20, batch:”ABC”};
delete student.age;Q27.What is an undefined value in JavaScript?
Ans-
Undefined value means the
Variable used in the code doesn’t exist
Variable is not assigned to any value
Property doesn’t existQ28.What are all the types of Pop up boxes available in JavaScript?
Ans-
Alert
Confirm and
PromptQ29.What is the use of Void(0)?
Ans-
Void(0) is used to prevent the page from refreshing and parameter “zero” is passed while calling.
–
Void(0) is used to call another method without refreshing the page.Q30.How can a page be forced to load another page in JavaScript?
Ans-
The following code has to be inserted to achieve the desired effect:
<script language=”JavaScript” type=”text/javascript” >Q31.What is the data type of variables of in JavaScript?
Ans-
All variables in the JavaScript are object data types.Q32.What is the difference between an alert box and a confirmation box?
Ans-
An alert box displays only one button which is the OK button.
–
But a Confirmation box displays two buttons namely OK and cancel.Q33.What are escape characters?
Ans-
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”Q34.What are Screen objects?
Ans-
Screen objects are used to read the information from the client’s screen. The properties of screen objects are –
AvailHeight: Gives the height of client’s screen
AvailWidth: Gives the width of client’s screen.
ColorDepth: Gives the bit depth of images on the client’s screen
Height: Gives the total height of the client’s screen, including the taskbar
Width: Gives the total width of the client’s screen, including the taskbarQ35.Explain the unshift() method ?
Ans-
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 –
var name = [ “john” ];
name.unshift( “charlie” );
name.unshift( “joseph”, “Jane” );
console.log(name);
The output is shown below:
[” joseph “,” Jane “, ” charlie “, ” john “]Related: Core Java Interview
JavaScript interview questions and answers for experienced
Q36.Define unescape() and escape() functions?
Ans-
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.
For Example:
<script>
document.write(escape(“Hello? How are you!”));
</script>
Output: Hello%3F%20How%20are%20you%21
The unescape() function is very important as it decodes the coded string.
It works in the following way. For example:
<script>
document.write(unescape(“Hello%3F%20How%20are%20you%21″));
</script>
Output: Hello? How are you!Q37.What are the decodeURI() and encodeURI()?
Ans-
EncodeURl() is used to convert URL into their hex coding. And DecodeURI() is used to convert the encoded URL back to normal.
<script>
var uri=”my test.asp?name=ståle&car=saab”;
document.write(encodeURI(uri)+ “<br>”);
document.write(decodeURI(uri));
</script>
Output –
my%20test.asp?name=st%C3%A5le&car=saab
my test.asp?name=ståle&car=saabQ38.Why it is not advised to use innerHTML in JavaScript?
Ans-
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.Q39.What does the following statement declares?
Ans-
var myArray = [[[]]];
It declares a three dimensional array.Q40.How are JavaScript and ECMA Script related?
Ans-
ECMA Script are like rules and guideline while Javascript is a scripting language used for web development.Q41.What is namespacing in JavaScript and how is it used?
Ans-
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.Q42.How can JavaScript codes be hidden from old browsers that don’t support JavaScript?
Ans-
For hiding JavaScript codes from old browsers:
Add “<!–” without the quotes in the code just after the <script> tag.
Add “//–>” without the quotes in the code just before the <script> tag.
Old browsers will now treat this JavaScript code as a long HTML comment. While, a browser that supports JavaScript, will take the “<!–” and “//–>” as one-line comments.Q43.How are object properties assigned?
Ans-
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”Q44.What is the method for reading and writing a file in JavaScript?
Ans-
This can be done by Using JavaScript extensions (runs from JavaScript Editor), example for opening of a file –
fh = fopen(getScriptPath(), 0);Q45.How are DOM utilized in JavaScript?
Ans-
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.Q46.How are event handlers utilized in JavaScript?
Ans-
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.Q47.Explain the role of deferred scripts in JavaScript?
Ans-
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.Q48.What are the various functional components in JavaScript?
Ans-
The different functional components in JavaScript are-
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.Q49.Write about the errors shown in JavaScript?
Ans-
JavaScript gives a message if it encounters an error. The recognized errors are –
–
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.Q50.What is the role of break and continue statements?
Ans-
Break statement is used to come out of the current loop while the continue statement continues the current loop with a new recurrence.
Conclusion –
Here’s a curated collection of critical JavaScript interview questions you should be familiar with. We trust that our list of JavaScript interview questions has provided valuable insights. If you found yourself unable to answer any of the questions, it’s a sign that you might need to dedicate more effort to enhancing your understanding of the subject. By thoroughly preparing, you’ll be well-equipped to excel in your upcoming JavaScript interview. Thank you for visiting us, and we wish you the best of luck in your next JavaScript interview!