Home Web DevelopmentExpress JS Top 50+ JavaScript Interview Questions

Top 50+ JavaScript Interview Questions

by Sikander Iqbal
Top 50+ JavaScript Interview Questions 1

JavaScript was created by Brendan Eich in 1995 to give web pages a little more pep than the <blink> tag could provide. Today it has far more powerful uses and companies like Google and Facebook use JavaScript to build complex desktop-like web applications. With the launch of Node.js, It has also become one of the most popular languages for building server-side software. The following are the interview questions of JavaScript: 

Q.1- What is JavaScript? 

Ans: JavaScript is a client-side as well as a 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 

Q.2- 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 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. 

Q.3- What are JavaScript Data Types? 

Ans: Following are the JavaScript Data types: 

  • Number 
  • String 
  • Boolean 
  • Object 
  • Undefined 

Q.4- What is the use of isNaN function? 

Ans: isNan function returns true if the argument is not a number otherwise it is false. 

Q.5- 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). 

Q.6- What is negative infinity? 

Ans: Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero. 

Q.7- Is it possible to break JavaScript Code into several lines? 

Ans: Breaking within a string statement can be done by the use of 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 break in line. 

Example: 

var x=1, y=2,z=x+y; 

The above code is perfectly fine, though not advisable as it hampers debugging. 

Q.8- Which company developed JavaScript? 

Ans: Netscape is the software company who developed JavaScript. 

Q.9- 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. 

Q.10- 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> 

Q.11- 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. 

Q.12- 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. 

Q.13- What is ‘this’ keyword in JavaScript? 

Ans: ‘This’ keyword refers to the object from where it was called. 

Q.14- 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. 

Q.15- Which symbol is used for comments in Javascript? 

Ans: // for Single line comments and 

/* Multi 

Line 

Comment 

*/ 

Q.16- 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. 

Q.17- 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. 

Q.18- 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(); 

Q.19- 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 

Q.20- 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“;  

Q.21- Explain how to read and write a file using JavaScript? 

AnsThere are two ways to read and write a file using JavaScript 

  • Using JavaScript extensions 
  • Using a web page and Active X objects 

Q.22- What are all the looping structures in JavaScript? 

Ans: Following are looping structures in Javascript: 

  • For 
  • While 
  • do-while loops 

Q.23- 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 exist 

Q.24- What are all the types of Pop up boxes available in JavaScript? 

  • Alert 
  • Confirm and 
  • Prompt 

Q.25- 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. 

Q.26- 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” ><!– location.href=”http://newhost/newpath/newfile.html”; //–></script> 

Q.27- What is the data type of variables of in JavaScript? 

Ans: All variables in the JavaScript are object data types. 

Q.28- 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. 

Q.29- 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” 

Q.30- What are JavaScript Cookies? 

Ans: 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. 

Q.31- Explain what is pop()method in JavaScript? 

Ans: 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 

Q.32- Whether JavaScript has concept level scope? 

Ans: No. JavaScript does not have concept level scope. The variable declared inside the function has scope inside the function. 

Q.33- Mention what is the disadvantage of using innerHTML in JavaScript? 

Ans: If you use innerHTML in JavaScript the disadvantage is 

Content is replaced everywhere 

We cannot use 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 build into elements, therefore its much slower 

The innerHTML does not provide validation and therefore we can potentially insert valid and broken HTML in the document and break it 

Q.34- What is break and continue statements? 

Ans: Break statement exits from the current loop. 

Continue statement continues with next statement of the loop. 

Q.35- What are the two basic groups of dataypes in JavaScript? 

Ans: They are as – 

  • Primitive 
  • Reference types. 

Primitive types are number and Boolean data types. Reference types are more complex types like strings and dates. 

Q.36- How generic objects can be created? 

Ans: Generic objects can be created as: 

var I = new object(); 

Q.37- What is the use of type of operator? 

Ans: ‘Typeof’ is an operator which is used to return a string description of the type of a variable. 

Q.38- Which keywords are used to handle exceptions? 

Ans: Try… Catch—finally is used to handle exceptions in the JavaScript 

Try{Code}Catch(exp){Code to throw an exception}Finally{Code runs either it finishes successfully or after catch} 

Q.39- Which keyword is used to print the text in the screen? 

Ans: document.write(“Welcome”) is used to print the text – Welcome in the screen. 

Q.40- What is the use of blur function? 

Ans: Blur function is used to remove the focus from the specified object. 

Q.41- What is variable typing? 

Ans: Variable typing is used to assign a number to a variable and then assign string to the same variable. Example is as follows: 

i= 8;i=”john”; 

Q.42- How to find operating system in the client machine using JavaScript? 

Ans: The ‘Navigator.appversion’ is used to find the name of the operating system in the client machine. 

Q.43- What are the different types of errors in JavaScript? 

Ans: There are three types of errors: 

  • 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. 

Q.44- What is the use of Push method in JavaScript? 

Ans: 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 

Q.45- What is unshift method in JavaScript? 

Ans: 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. 

Q.46- What is the difference between JavaScript and Jscript? 

Ans: Both are almost similar. JavaScript is developed by Netscape and Jscript was developed by Microsoft. 

Q.47- What is the ‘Strict’ mode in JavaScript and how can it be enabled? 

Ans: Strict mode is a way to introduce better error-checking into your code. 

When you use strict mode, you cannot use implicitly declared variables, or assign a value to a read-only property, or add a property to an object that is not extensible. 

You can enable strict mode by adding “use strict” at the beginning of a file, a program, or a function. 

Q.48- What is a prompt box in JavaScript? 

Ans: A prompt box is a box which allows the user to enter input by providing a text box. The prompt() method displays a dialog box that prompts the visitor for input. A prompt box is often used if you want the user to input a value before entering a page. When a prompt box pops up, the user will have to click either “OK” or “Cancel” to proceed after entering an input value. 

Q.49- What is NaN in JavaScript? 

Ans: NaN is a short form of Not a Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number. When a string or something else is being converted into a number and that cannot be done, then we get to see NaN. 

Q.50- What is the difference between undeclared & undefined? 

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. 

0 comment

You may also like

Leave a Comment