JavaScript Interview Questions and Answers

05 Nov, 2019
What are the differences between Java and JavaScript ?JavaScript is a client-side scripting language and Java is object Oriented Programming language, both of them are totally different from each other.JavaScript: It is a light-weighted programming language (“scripting language”) and used to develop interactive web pages. It can insert dynamic text into HTML element. JavaScript is also known as the browser’s language.Java: Java is one of the most popular and widely used programming language. It is an object-oriented programming language and has a virtual machine platform that allows you to create compiled programs that run on nearly every platform. Java promised, “Write Once, Run Anywhere”.   What are JavaScript Data Types ?There are three major Datatypes in JavaScript.PrimitiveNumbersStringsBooleanTrivialNullUndefinedCompositeObjectsFunctionsArrays   Which symbol is used for comments in JavaScript ?Comments are used to prevent the execution of statements. Comments are ignored while the compiler executes the code. There are two type of symbols used to represent comment in JavaScript:Double slash: It is known as single line comment.// Single line commentSlash with Asterisk: It is known as multi-line comment./* 
Multi-line comments
...
*/   What would be the result of 3+2+”7′′ ?Here 3 and 2 behave like an integer and “7” behaves like a string. So 3 plus 2 will 5. Then the output will be 5+”7′′ = 57.   What is the use of isNaN function ?The Number.isNan function in JavaScript is used to determines whether the passed value is NaN (Not a Number) and is of the type “Number”. In JavaScript, the value NaN is considered a type of number. It returns true if the argument is not a number else it returns false.   Which is faster in JavaScript and ASP script ?The JavaScript is faster compare to ASP Script because JavaScript is a client-side scripting language and does not depend on the server to execute it but the ASP script is a server-side scripting language always dependable on the server.   What is negative infinity ?The negative infinity in JavaScript is a constant value that is used to represent the lowest available value. It means that no other number is lesser than this value. It can be generated using a self-made function or by an arithmetic operation. JavaScript shows the NEGATIVE_INFINITY value as -Infinity.   Is it possible to break JavaScript Code into several lines ?Yes, it is possible to break the JavaScript code into several lines in string statement. It can be break by using the backslash ‘\’. For example:document.write("A Online Computer Science Portal\ for Geeks")The code-breaking line is avoided by JavaScript which is not preferable.var gfg= 10, GFG = 5,
Geeks =
gfg + GFG;   Which company developed JavaScript ?Netscape developed the JavaScript and created by Brenden Eich in the year of 1995.   What are undeclared and undefined variables ?Undefined: It occurs when a variable has been declared but has not been assigned with any value. Undefined is not a keyword.Undeclared: It occurs when we try to access any variable which is not initialized or declared earlier using var or const keyword. If we use ‘typeof’ operator to get the value of an undeclared variable, we will face the runtime error with return value as “undefined”. The scope of the undeclared variables is always global.   Write a JavaScript code for adding new elements dynamically ?<!DOCTYPE html><html>  <head>    <title>        JavaScript code for adding new        elements dynamically    </title></head>  <body>    <button onclick="create()">        Click Here!    </button>          <script>        function create() {            var geeks = document.createElement('geeks');            geeks.textContent = "Geeksforgeeks";            geeks.setAttribute('class', 'note');            document.body.appendChild(geeks);        }    </script></body>  </html>   What are global variables? How are these variable declared and what are the problems associated with them ?In contrast, global variables are the variables that are defined outside of functions. These variables have a global scope, so they can be used by any function without passing them to the function as parameters.Example:<script>      var petName = "Rocky"; //Global Variable      myFunction();          function myFunction() {          document.getElementById("geeks").innerHTML                      = typeof petName + "- " +                       "My pet name is " + petName;      }          document.getElementById("Geeks").innerHTML                      = typeof petName + "- " +                       "My pet name is " + petName; </script> It is difficult to debug and test the code that relies on global variables.   What do you mean by NULL in JavaScript ?The NULL value represents the no value or no object. It can be called as empty value/object.   How to delete property specific value ?The delete keyword is used to delete the whole property and all the values at once likevar gfg={Course: "DSA", Duration:30};
delete gfg.Course;   What is a prompt box ?It is used to display a dialog box with an optional message prompting the user to input some text. It is often used if the user wants to input a value before entering a page. It returns a string containing the text entered by the user, or null.   What is ‘this’ keyword in JavaScript ?Functions in JavaScript are essential objects. Like objects, they can be assigned to variables, passed to other functions and returned from functions. And much like objects, they have their own properties.‘this’ stores the current execution context of the JavaScript program. Thus, when it used inside a function, the value of ‘this’ will change depending on how the function is defined, how it is invoked and the default execution context.   Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any ?Timer is used to execute some specific code at specific time or any small amount of code in repetition to do that you need to use the function setTimout, setInterval and clearInterval. If JavaScript code set the timer of 2 minutes and when the times up then the page display an alert message “times up”. The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.   What is the difference between ViewState and SessionState ?ViewState: It is specific to a single page in a session.SessionState: It is user specific that can access all the data in the webpages.   How can you submit a form using JavaScript ?You can use document.form[0].submit() method to submit the form in JavaScript.   Does JavaScript support automatic type conversion ?Yes, JavaScript supports automatic type conversion.
What are the differences between Java and JavaScript ?JavaScript is a client-side scripting language and Java is object Oriented Programming language, both of them are totally different from each other.JavaScript: It is a light-weighted programming language (“scripting language”) and used to develop interactive web pages. It can insert dynamic text into HTML element. JavaScript is also known as the browser’s language.Java: Java is one of the most popular and widely used programming language. It is an object-oriented programming language and has a virtual machine platform that allows you to create compiled programs that run on nearly every platform. Java promised, “Write Once, Run Anywhere”.
JavaScript: It is a light-weighted programming language (“scripting language”) and used to develop interactive web pages. It can insert dynamic text into HTML element. JavaScript is also known as the browser’s language.
Java: Java is one of the most popular and widely used programming language. It is an object-oriented programming language and has a virtual machine platform that allows you to create compiled programs that run on nearly every platform. Java promised, “Write Once, Run Anywhere”.
   
What are JavaScript Data Types ?There are three major Datatypes in JavaScript.PrimitiveNumbersStringsBooleanTrivialNullUndefinedCompositeObjectsFunctionsArrays
PrimitiveNumbersStringsBoolean
Numbers
Strings
Boolean
TrivialNullUndefined
Null
Undefined
CompositeObjectsFunctionsArrays
Objects
Functions
Arrays
   
Which symbol is used for comments in JavaScript ?Comments are used to prevent the execution of statements. Comments are ignored while the compiler executes the code. There are two type of symbols used to represent comment in JavaScript:Double slash: It is known as single line comment.// Single line commentSlash with Asterisk: It is known as multi-line comment./* 
Multi-line comments
...
*/
Double slash: It is known as single line comment.// Single line comment
// Single line comment
Slash with Asterisk: It is known as multi-line comment./* 
Multi-line comments
...
*/
/* 
Multi-line comments
...
*/
   
What would be the result of 3+2+”7′′ ?Here 3 and 2 behave like an integer and “7” behaves like a string. So 3 plus 2 will 5. Then the output will be 5+”7′′ = 57.
   
What is the use of isNaN function ?The Number.isNan function in JavaScript is used to determines whether the passed value is NaN (Not a Number) and is of the type “Number”. In JavaScript, the value NaN is considered a type of number. It returns true if the argument is not a number else it returns false.
   
Which is faster in JavaScript and ASP script ?The JavaScript is faster compare to ASP Script because JavaScript is a client-side scripting language and does not depend on the server to execute it but the ASP script is a server-side scripting language always dependable on the server.
   
What is negative infinity ?The negative infinity in JavaScript is a constant value that is used to represent the lowest available value. It means that no other number is lesser than this value. It can be generated using a self-made function or by an arithmetic operation. JavaScript shows the NEGATIVE_INFINITY value as -Infinity.
   
Is it possible to break JavaScript Code into several lines ?Yes, it is possible to break the JavaScript code into several lines in string statement. It can be break by using the backslash ‘\’. For example:document.write("A Online Computer Science Portal\ for Geeks")The code-breaking line is avoided by JavaScript which is not preferable.var gfg= 10, GFG = 5,
Geeks =
gfg + GFG;
document.write("A Online Computer Science Portal\ for Geeks")
The code-breaking line is avoided by JavaScript which is not preferable.
var gfg= 10, GFG = 5,
Geeks =
gfg + GFG;
   
Which company developed JavaScript ?Netscape developed the JavaScript and created by Brenden Eich in the year of 1995.
   
What are undeclared and undefined variables ?Undefined: It occurs when a variable has been declared but has not been assigned with any value. Undefined is not a keyword.Undeclared: It occurs when we try to access any variable which is not initialized or declared earlier using var or const keyword. If we use ‘typeof’ operator to get the value of an undeclared variable, we will face the runtime error with return value as “undefined”. The scope of the undeclared variables is always global.
Undefined: It occurs when a variable has been declared but has not been assigned with any value. Undefined is not a keyword.
Undeclared: It occurs when we try to access any variable which is not initialized or declared earlier using var or const keyword. If we use ‘typeof’ operator to get the value of an undeclared variable, we will face the runtime error with return value as “undefined”. The scope of the undeclared variables is always global.
   
Write a JavaScript code for adding new elements dynamically ?<!DOCTYPE html><html>  <head>    <title>        JavaScript code for adding new        elements dynamically    </title></head>  <body>    <button onclick="create()">        Click Here!    </button>          <script>        function create() {            var geeks = document.createElement('geeks');            geeks.textContent = "Geeksforgeeks";            geeks.setAttribute('class', 'note');            document.body.appendChild(geeks);        }    </script></body>  </html>
<!DOCTYPE html><html>  <head>    <title>        JavaScript code for adding new        elements dynamically    </title></head>  <body>    <button onclick="create()">        Click Here!    </button>          <script>        function create() {            var geeks = document.createElement('geeks');            geeks.textContent = "Geeksforgeeks";            geeks.setAttribute('class', 'note');            document.body.appendChild(geeks);        }    </script></body>  </html>
   
What are global variables? How are these variable declared and what are the problems associated with them ?In contrast, global variables are the variables that are defined outside of functions. These variables have a global scope, so they can be used by any function without passing them to the function as parameters.Example:<script>      var petName = "Rocky"; //Global Variable      myFunction();          function myFunction() {          document.getElementById("geeks").innerHTML                      = typeof petName + "- " +                       "My pet name is " + petName;      }          document.getElementById("Geeks").innerHTML                      = typeof petName + "- " +                       "My pet name is " + petName; </script> It is difficult to debug and test the code that relies on global variables.
<script>      var petName = "Rocky"; //Global Variable      myFunction();          function myFunction() {          document.getElementById("geeks").innerHTML                      = typeof petName + "- " +                       "My pet name is " + petName;      }          document.getElementById("Geeks").innerHTML                      = typeof petName + "- " +                       "My pet name is " + petName; </script> 
It is difficult to debug and test the code that relies on global variables.
   
What do you mean by NULL in JavaScript ?The NULL value represents the no value or no object. It can be called as empty value/object.
   
How to delete property specific value ?The delete keyword is used to delete the whole property and all the values at once likevar gfg={Course: "DSA", Duration:30};
delete gfg.Course;
var gfg={Course: "DSA", Duration:30};
delete gfg.Course;
   
What is a prompt box ?It is used to display a dialog box with an optional message prompting the user to input some text. It is often used if the user wants to input a value before entering a page. It returns a string containing the text entered by the user, or null.
   
What is ‘this’ keyword in JavaScript ?Functions in JavaScript are essential objects. Like objects, they can be assigned to variables, passed to other functions and returned from functions. And much like objects, they have their own properties.‘this’ stores the current execution context of the JavaScript program. Thus, when it used inside a function, the value of ‘this’ will change depending on how the function is defined, how it is invoked and the default execution context.
   
Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any ?Timer is used to execute some specific code at specific time or any small amount of code in repetition to do that you need to use the function setTimout, setInterval and clearInterval. If JavaScript code set the timer of 2 minutes and when the times up then the page display an alert message “times up”. The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds.
   
What is the difference between ViewState and SessionState ?ViewState: It is specific to a single page in a session.SessionState: It is user specific that can access all the data in the webpages.
ViewState: It is specific to a single page in a session.
SessionState: It is user specific that can access all the data in the webpages.
   
How can you submit a form using JavaScript ?You can use document.form[0].submit() method to submit the form in JavaScript.
   
Does JavaScript support automatic type conversion ?Yes, JavaScript supports automatic type conversion.
Related Article: Commonly asked JavaScript Interview Questions | Set 1
interview-preparation
Marketing
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Remove elements from a JavaScript Array
How to append HTML code to a div using JavaScript ?
Difference Between PUT and PATCH Request
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?