Javascript Interview Questions And Answer

This website contains Javascript interview questions with answers. We have given Javascript interview questions faced by freshers and experienced in real interviews in IT industries.Dear readers, these Javascript Programming Interview questions have been specially designed so that you can get acquainted with the nature of the questions you may be ask during your interview.


1. What is JavaScript?

Answer:-An object-oriented computer programming language commonly used to create interactive effects within web browsers.
Javascript (JS) is a scripting language, which is mainly used on the web. It is used to increase HTML pages and is usually found embedded in HTML code. Javascript is an interpreted language. Thus, it does not need to be compiled. Javascript presents web pages in an interactive and dynamic fashion.It allows pages to respond to events, display special effects, accept variable text, validate data, make cookies, detect user's browser, and more.


2. Enumerate the differences between Java and JavaScript?

Answer:- There are lot of differences, some of the major differences are:

1)  The JavaScript programming language is developed by Netscape, Inc and not part of the Java platform.
2) Java applications are run in a virtual machine or web browser while JavaScript is run on a web browser.
3)  Java code is compiled whereas while JavaScript code is in text and in a web page.
4)  JavaScript is an OOP scripting language, whereas Java is an OOP programming language.
5) Java has a thread based approach to concurrency While Javascript has event based approach to concurrency.
6)  Java program uses more memory while JavaScript requires less memory therefore it is used in web pages.
7) Java is a Standalone laguage While Javascript contained within a web page and integrates with its HTML content.


3. What are JavaScript Data Types?

Answer:- JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.

 Primitive data type: There are five types of primitive data types in JavaScript:-
  1. String
  2. Number
  3. Boolean
  4. Undefined
  5. NULL
 Non-primitive (reference) data types: There are three non-primitive data types:-
  1. Object
  2. Array
  3. RegExp


4. What is the use of isNaN function?

Answer:- The IsNaN () function determines whether the value is an invalid number (not-a-number). If the value is equal to NaN, then this function is true. Otherwise it is false. This function number is different from the specific number .isNaN () method.


5. What is negative infinity?

Answer:- Negative infinity is a number in JavaScript, which is derived from 'dividing negative numbers by zero'.
- A number object should not be created to access this static property.
- The value of negative infinity is similar to the negative value of the infinite property of the global object.


6.Is it possible to break JavaScript Code into several lines?

Answer:- Within a string statement at the end of the first line, We use the breaking backslash '\' to break JavaScript Code into several lines.
For Example :- document . write("This is \a program");


7. Which company developed JavaScript?

Answer:- When Eich created Javascript in 1995, they created it for Netscape Navigator and it soon became known as the Livescript. In another quirk of javascript history, the team changed the name of JavaScript to reflect the support of Java's Netscape within its browser.


8. What are global variables in javascript?

Answer:- Global variables are those that are available during the whole length of the code, i.e. there is no scope for it. The variable keyword is used to declare local variables or objects. If the variable keyword is omitted, then a global variable is declared.
For Example:
  // Declare a global globalVariable = "letsfindcourse";


9. What is a prompt box?

Answer:- The prompt () method displays a dialog box which inspires the visitor to input. A prompt box is often used if you want to give value to the user before entering the page. Note: When a prompt box is popped up, the user must either click on "OK" or "Cancel" to proceed with entering the input value.


10. What is 'this' keyword in JavaScript?

Answer:- "This" keyword refers to an object, an object that is executing the current bit of JavaScript code.


11. Explain the working of timers in JavaScript?

Answer:- The timer is used to execute a piece of code in a set time, or to repeat the code in a given interval. This function is set by using the timeout, set interval and clear interval.
Timers operate within a single thread, and in such a way the events can be queued, waiting to be executed.


12. Which symbol is used for comments in Javascript?

Answer:-symbol is used for comments in Javascript is:-

  // for Single line comments and
  /* Multi
  Line
  Comment
  */


13. What is === operator?

Answer:-=== is called as a strict equality operator, which become true when both operands have the same value without any kind of conversion.


14. Explain how can you submit a form using JavaScript?

Answer:- To submit a form for javascript we use :-
document.form[0].submit();
document.form[0].submit();


15. Does JavaScript support automatic type conversion?

Answer:-Yes, Javascript supports automatic type conversion, this is a common way of conversion of types used by JavaScript developers.


16. Does constructor return any value?

Answer:-yes, The constructor implicitly returns the current instance of the class.
(You can't use an explicit return type with the constructor)
Javascript is a "loosely typed" language, which means that whenever an operator or statement is expecting a particular data-type, then Javascript will automatically convert the data into that type.


17. How can the style/class of an element be changed?

Answer:- It can be done in the following manner:
  document . getElementById("myText").style . fontSize = "10?;
  or
  document . getElementById("myText").className = "anyoftheclass";


18.Explain how to read and write a file using JavaScript?

Answer:- There are mainly two ways through which we can read and write the file using Javascript language :-
  1. We can use "Javascript Extensions" to read and write a file using JavaScript.
  2. Another way is we ca use "Using a Web Page and ActiveX Objects" to read and write a file using JavaScript.


19. What are all the looping structures in JavaScript?

Answer:- Many times when you write a code, you want to run the same code multiple times. To do this, you can use a looping statement in your code.
In Javascript we have the following looping statements:
1) For :- A block of code will be specified multiple times for the statement
2) While :- While statement will execute one block of code while one condition is true.
3) do-while :- do-while statement will execute a block of code once, and then it will repeat the loop, while one condition is true.


20. What is called Variable typing in Javascript?

Answer:-Variable typing is used to assign a number to a variable and the same variable can be assigned to a string.
The type of a variable is determined only when a value is assigned and the change can occur because the variable appears in different contexts


21. Explain the difference between "==" and "==="?

Answer:-By using = you assign a value to something.
  x = 1 //x now equals 1
  x = 2 //x now equals 2

By using == you check if something is equal to something else. This is not strict
  x == 1 //is x equal to 1? (False)
  x == 2 //is x equal to 2? (True)
  true == 1 //does the boolean value of true equal 1? (True)

By using === you check if something is equal to something else. This is also strict.
  x === 1 //is x equal to 1? (False)
  x === 2 //is x equal to 2? (True)
  true === 1 //does the boolean value of true equal 1? (False)


22. Explain the for-in loop??

Answer:- For-in loop is used to loop through the properties of the object.
The syntax for the loop is -
  for (variable name in object){
     statement or block to execute
  }
In each iteration, an object from the object is attached to the variable name, and the loop continues until all the properties of the object are finished.


23. What would be the result of 5+3+"9"?

Answer:-Since 5 and 3 are integers, they will be numerically added. And since 9 is a string, so it will be combined. So the result would be 89.


24. What do mean by NULL in Javascript?

Answer:-NULL value is used to represent no value or no object. It means that there is no object or a null string, a valid boolean value, no number, and no array object.


25. What is the function of delete operator?

Answer:-The Delete keyword is also used to remove the property along with its value.

For Example :-
  var student= {age:20, batch:"XYZ"};
  delete student.age;


26. What are all the types of Pop up boxes available in JavaScript?

Answer:-ll the types of Pop up boxes available in JavaScriptis :-
  1. Alert
  2. Confirm
  3. Prompt


27. Whether JavaScript has concept level scope?

Answer:- No. Javascript does not have concept level scope. There is scope inside the declared variable function inside the function.


28. What is break and continue statements?

Answer:-When a break statement is encountered, it eliminates the block and removes the control from the switch or loop. When a continue statement, it receives control over the next iteration of the loop. Due to a break, the innermost enclosing loop or switch immediately goes out.


29. How generic objects can be created?

Answer:-Generic objects can be created as:
  var I = new object();


30. Which keywords are used to handle exceptions?

Answer:-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
  }




Also check :


Discussion


* You must be logged in to add comment.