Looking for JavaScript interview questions? Well, you are at the right place. If you are going to take an interview for any JavaScript job then you would find this article very helpful. See no matter how well-read you are or how qualified you are, going to an interview is never easy. You can be nervous or anxious about what you should wear, how you should answer the questions, and most importantly what kind of questions they would ask in the interview. It doesn’t matter if you are a fresher or experienced candidate, these things would always be on your mind.
Since you are here to seek some help, we are not going to disappoint you. Here we have a list of some of the most common JavaScript interview questions that you should know about. Give a read to these questions and see if you can answer them. If you can then you are going great and you would do great in the interview as well. On the other hand, if you are unable to answer these questions, then don’t worry there is always time to improve yourself and prepare yourself for the upcoming interview. Go ahead and take a look at the following JavaScript interview questions and be ready for your JavaScript interview.
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 language
Q2.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:–NumberStringBooleanObjectUndefined
Q4.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 developers
Q11.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?;ordocument.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 extensionsUsing a web page and Active X objects
Q13.What are all the looping structures in JavaScript?Ans-Following are looping structures in Javascript:ForWhiledo-while loops
Q14.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.
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/* MultiLineComment*/
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.Examplevar student= {age:20, batch:”ABC”};delete student.age;
Q27.What is an undefined value in JavaScript?Ans-Undefined value means theVariable used in the code doesn’t existVariable is not assigned to any valueProperty doesn’t exist
Q28.What are all the types of Pop up boxes available in JavaScript?Ans-AlertConfirm andPrompt
Q29.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 screenAvailWidth: Gives the width of client’s screen.ColorDepth: Gives the bit depth of images on the client’s screenHeight: Gives the total height of the client’s screen, including the taskbarWidth: Gives the total width of the client’s screen, including the taskbar
Q35.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 “]
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%21The 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=saabmy test.asp?name=ståle&car=saab
Q38.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 –
So these are some of the most important JavaScript interview questions that you need to know about. We hope that you found our list of JavaScript interview questions helpful. If you were unable to answer the aforementioned interview questions then you should put more effort into your knowledge about the subject and be fully prepared for the interview. Thank you for visiting us and hope you will do great on your next JavaScript Interview.