{"id":4345,"date":"2015-06-12T04:35:23","date_gmt":"2015-06-12T04:35:23","guid":{"rendered":"http:\/\/a1webdesignteam.com\/blog\/?p=4345"},"modified":"2015-06-12T04:40:28","modified_gmt":"2015-06-12T04:40:28","slug":"javascript-interview-questions","status":"publish","type":"post","link":"https:\/\/a1webdesignteam.com\/blog\/javascript-interview-questions\/","title":{"rendered":"JavaScript Interview Questions"},"content":{"rendered":"<p><strong>1. What is JavaScript?<\/strong><\/p>\n<p>JavaScript is a client-side scripting language that can be inserted into HTML pages and is understood by web browsers.<\/p>\n<p><strong>2. Enumerate the differences between Java and JavaScript?<\/strong><\/p>\n<p>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.\u00a0 Java is an object \u2013 oriented programming (OOPS) or structured programming language like C++ or C whereas JavaScript is a client-side scripting language and it is said to be unstructured programming.<\/p>\n<p><strong>3. What are JavaScript types?<\/strong><\/p>\n<p>Following are the JavaScript types:<\/p>\n<ul>\n<li>Number<\/li>\n<li>String<\/li>\n<li>Boolean<\/li>\n<li>Function<\/li>\n<li>Object<\/li>\n<li>Null<\/li>\n<li>Undefined<\/li>\n<\/ul>\n<p><strong>4. What is the use of isNaN function?<\/strong><\/p>\n<p>isNan function returns true if the argument is not a number otherwise it is false.<\/p>\n<p><strong>5. Between JavaScript and an ASP script, which is faster?<\/strong><\/p>\n<p>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.<\/p>\n<p><a class=\"lightbox-added\" href=\"http:\/\/career.guru99.com\/wp-content\/uploads\/2013\/10\/javascript-code-snippet.gif\"><img loading=\"lazy\" class=\"size-medium wp-image-2489 \" title=\"Javascript\" src=\"http:\/\/careerguru991.revolutionventur.netdna-cdn.com\/wp-content\/uploads\/2013\/10\/javascript-code-snippet-300x216.gif\" alt=\"javascript-code-snippet\" width=\"300\" height=\"216\" \/><\/a><\/p>\n<p><strong>6. What is negative infinity?<\/strong><\/p>\n<p>Negative Infinity is a number in JavaScript which can be derived by dividing negative number by zero.<\/p>\n<p><strong>7. Is it possible to break JavaScript Code into several lines?<\/strong><\/p>\n<p>Breaking within a string statement can be done by the use of a backslash, \u2018\\\u2019, at the end of the first line<\/p>\n<p>Example:<\/p>\n<div id=\"crayon-55752ae7c4926500535132\" class=\"crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-plain-wrap\"><textarea class=\"crayon-plain print-no\" readonly=\"readonly\" wrap=\"soft\" data-settings=\"dblclick\">document.write(&#8220;This is \\a program&#8221;);<\/textarea><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c4926500535132-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-55752ae7c4926500535132-1\" class=\"crayon-line\"><span class=\"crayon-v\">document<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">write<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;This is \\a program&#8221;<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">;<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>And if you change to a new line when not within a string statement, then javaScript ignores break in line.<\/p>\n<p>Example:<\/p>\n<div id=\"crayon-55752ae7c492f219914183\" class=\"crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-plain-wrap\"><textarea class=\"crayon-plain print-no\" readonly=\"readonly\" wrap=\"soft\" data-settings=\"dblclick\">var x=1, y=2,<\/p>\n<p>z=<\/p>\n<p>x+y;<\/textarea><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c492f219914183-1\">1<\/div>\n<div class=\"crayon-num crayon-striped-num\" data-line=\"crayon-55752ae7c492f219914183-2\">2<\/div>\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c492f219914183-3\">3<\/div>\n<div class=\"crayon-num crayon-striped-num\" data-line=\"crayon-55752ae7c492f219914183-4\">4<\/div>\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c492f219914183-5\">5<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-55752ae7c492f219914183-1\" class=\"crayon-line\"><span class=\"crayon-t\">var<\/span> <span class=\"crayon-v\">x<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-cn\">1<\/span><span class=\"crayon-sy\">,<\/span> <span class=\"crayon-v\">y<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-cn\">2<\/span><span class=\"crayon-sy\">,<\/span><\/div>\n<div id=\"crayon-55752ae7c492f219914183-2\" class=\"crayon-line crayon-striped-line\"><\/div>\n<div id=\"crayon-55752ae7c492f219914183-3\" class=\"crayon-line\"><span class=\"crayon-v\">z<\/span><span class=\"crayon-o\">=<\/span><\/div>\n<div id=\"crayon-55752ae7c492f219914183-4\" class=\"crayon-line crayon-striped-line\"><\/div>\n<div id=\"crayon-55752ae7c492f219914183-5\" class=\"crayon-line\"><span class=\"crayon-v\">x<\/span><span class=\"crayon-o\">+<\/span><span class=\"crayon-v\">y<\/span><span class=\"crayon-sy\">;<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The above code is perfectly fine, though not advisable as it hampers debugging.<\/p>\n<p><strong>8. Which company developed JavaScript?<\/strong><\/p>\n<p>Netscape is the software company who developed JavaScript.<\/p>\n<p><strong>9. What are undeclared and undefined variables?<\/strong><\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><strong>10. Write the code for adding new elements dynamically?<\/strong><\/p>\n<div id=\"crayon-55752ae7c4932561489422\" class=\"crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-plain-wrap\"><textarea class=\"crayon-plain print-no\" readonly=\"readonly\" wrap=\"soft\" data-settings=\"dblclick\">&lt;html&gt;<br \/>\n&lt;head&gt; &lt;title&gt;t1&lt;\/title&gt;<br \/>\n&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nfunction addNode() { var newP = document.createElement(&#8220;p&#8221;);<br \/>\nvar textNode = document.createTextNode(&#8221; This is a new text node&#8221;);<br \/>\nnewP.appendChild(textNode); document.getElementById(&#8220;firstP&#8221;).appendChild(newP); }<br \/>\n&lt;\/script&gt; &lt;\/head&gt;<br \/>\n&lt;body&gt; &lt;p id=&#8221;firstP&#8221;&gt;firstP&lt;p&gt; &lt;\/body&gt;<br \/>\n&lt;\/html&gt;<\/textarea><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c4932561489422-1\">1<\/div>\n<div class=\"crayon-num crayon-striped-num\" data-line=\"crayon-55752ae7c4932561489422-2\">2<\/div>\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c4932561489422-3\">3<\/div>\n<div class=\"crayon-num crayon-striped-num\" data-line=\"crayon-55752ae7c4932561489422-4\">4<\/div>\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c4932561489422-5\">5<\/div>\n<div class=\"crayon-num crayon-striped-num\" data-line=\"crayon-55752ae7c4932561489422-6\">6<\/div>\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c4932561489422-7\">7<\/div>\n<div class=\"crayon-num crayon-striped-num\" data-line=\"crayon-55752ae7c4932561489422-8\">8<\/div>\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c4932561489422-9\">9<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-55752ae7c4932561489422-1\" class=\"crayon-line\"><span class=\"crayon-r \">&lt;html&gt;<\/span><\/div>\n<div id=\"crayon-55752ae7c4932561489422-2\" class=\"crayon-line crayon-striped-line\"><span class=\"crayon-r \">&lt;head&gt;<\/span> <span class=\"crayon-r \">&lt;title&gt;<\/span><span class=\"crayon-i \">t1<\/span><span class=\"crayon-r \">&lt;\/title&gt;<\/span><\/div>\n<div id=\"crayon-55752ae7c4932561489422-3\" class=\"crayon-line\"><span class=\"crayon-i \"><span class=\"crayon-ta\">&lt;script <\/span><span class=\"crayon-e \">type<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s\">&#8220;text\/javascript&#8221;<\/span><span class=\"crayon-o\">&gt;<\/span> <\/span><\/div>\n<div id=\"crayon-55752ae7c4932561489422-4\" class=\"crayon-line crayon-striped-line\"><span class=\"crayon-t\">function<\/span> <span class=\"crayon-e\">addNode<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-sy\">)<\/span> <span class=\"crayon-sy\">{<\/span> <span class=\"crayon-t\">var<\/span> <span class=\"crayon-v\">newP<\/span> <span class=\"crayon-o\">=<\/span> <span class=\"crayon-v\">document<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">createElement<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;p&#8221;<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">;<\/span><\/div>\n<div id=\"crayon-55752ae7c4932561489422-5\" class=\"crayon-line\"><span class=\"crayon-t\">var<\/span> <span class=\"crayon-v\">textNode<\/span> <span class=\"crayon-o\">=<\/span> <span class=\"crayon-v\">document<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">createTextNode<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8221; This is a new text node&#8221;<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">;<\/span><\/div>\n<div id=\"crayon-55752ae7c4932561489422-6\" class=\"crayon-line crayon-striped-line\"><span class=\"crayon-v\">newP<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">appendChild<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">textNode<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">;<\/span> <span class=\"crayon-v\">document<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">getElementById<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-s\">&#8220;firstP&#8221;<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">appendChild<\/span><span class=\"crayon-sy\">(<\/span><span class=\"crayon-v\">newP<\/span><span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">;<\/span> <span class=\"crayon-sy\">}<\/span><\/div>\n<div id=\"crayon-55752ae7c4932561489422-7\" class=\"crayon-line\"><span class=\"crayon-ta\">&lt;\/script&gt;<\/span> <span class=\"crayon-r \">&lt;\/head&gt;<\/span><\/div>\n<div id=\"crayon-55752ae7c4932561489422-8\" class=\"crayon-line crayon-striped-line\"><span class=\"crayon-r \">&lt;body&gt;<\/span> <span class=\"crayon-r \">&lt;p <\/span><span class=\"crayon-e \">id<\/span><span class=\"crayon-o\">=<\/span><span class=\"crayon-s \">&#8220;firstP&#8221;<\/span><span class=\"crayon-r \">&gt;<\/span><span class=\"crayon-i \">firstP<\/span><span class=\"crayon-r \">&lt;p&gt;<\/span> <span class=\"crayon-r \">&lt;\/body&gt;<\/span><\/div>\n<div id=\"crayon-55752ae7c4932561489422-9\" class=\"crayon-line\"><span class=\"crayon-r \">&lt;\/html&gt;<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><strong>11. What are global variables?<\/strong><strong> How are these variable declared and what are the problems associated with using them?<\/strong><\/p>\n<p>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.<\/p>\n<p>Example:<\/p>\n<p>\/\/ Declare a global globalVariable = \u201cTest\u201d;<\/p>\n<p>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.<\/p>\n<p><strong>12. What is a prompt box?<\/strong><\/p>\n<p>A prompt box is a box which allows the user to enter input by providing a text box.\u00a0 Label and box will be provided to enter the text or number.<\/p>\n<p><strong>13. What is \u2018this\u2019 keyword in JavaScript?<\/strong><\/p>\n<p>\u2018This\u2019 keyword is used to point at the current object in the code. For instance: If the code is presently at an object created by the help of the \u2018new\u2019 keyword, then \u2018this\u2019 keyword will point to the object being created.<\/p>\n<p><strong>14. Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if any?<\/strong><\/p>\n<p>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\u00a0<strong>setTimeout, setInterval\u00a0<\/strong>and<strong>\u00a0clearInterval<\/strong>.<\/p>\n<p>The\u00a0<strong>setTimeout(function, delay)<\/strong>\u00a0function is used to start a timer that calls a particular function after the mentioned delay. The\u00a0<strong>setInterval(function, delay)<\/strong>\u00a0function is used to repeatedly execute the given function in the mentioned delay and only halts when cancelled. The\u00a0<strong>clearInterval(id)<\/strong>\u00a0function instructs the timer to stop.<\/p>\n<p>Timers are operated within a single thread, and thus events might queue up, waiting to be executed.<\/p>\n<p><strong>15. Which symbol is used for comments in Javascript?<\/strong><\/p>\n<p>\/\/ for Single line comments and<\/p>\n<p>\/* \u00a0 Multi<\/p>\n<p>Line<\/p>\n<p>Comment<\/p>\n<p>*\/<\/p>\n<p><strong>16. What is the difference between ViewState and SessionState?<\/strong><\/p>\n<p>\u2018ViewState\u2019 is specific to a page in a session.<\/p>\n<p>\u2018SessionState\u2019 is specific to user specific data that can be accessed across all pages in the web application.<\/p>\n<p><strong>17. What is === operator?<\/strong><\/p>\n<p>=== is called as strict equality operator which returns true when the two operands are having the same value without any type conversion.<\/p>\n<p><strong>18.\u00a0<\/strong><strong>Explain how can you submit a form using JavaScript?<\/strong><\/p>\n<p>To submit a form using JavaScript use document.form[0].submit();<\/p>\n<p>document.form[0].submit();<\/p>\n<p><strong>19.\u00a0<\/strong><strong>Does JavaScript support automatic type conversion?<\/strong><\/p>\n<p>Yes JavaScript does support automatic type conversion, it is the common way of type conversion used by JavaScript developers<\/p>\n<p><strong>20. How can the style\/class of an element be changed?<\/strong><\/p>\n<p>It can be done in the following way:<\/p>\n<div id=\"crayon-55752ae7c4951163178501\" class=\"crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-plain-wrap\"><textarea class=\"crayon-plain print-no\" readonly=\"readonly\" wrap=\"soft\" data-settings=\"dblclick\">document.getElementById(\u201cmyText\u201d).style.fontSize = \u201c20?;<\/textarea><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c4951163178501-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-55752ae7c4951163178501-1\" class=\"crayon-line\"><span class=\"crayon-v\">document<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">getElementById<\/span><span class=\"crayon-sy\">(<\/span>\u201c<span class=\"crayon-i\">myText<\/span>\u201d<span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">style<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">fontSize<\/span> <span class=\"crayon-o\">=<\/span> \u201c<span class=\"crayon-cn\">20<\/span><span class=\"crayon-sy\">?<\/span><span class=\"crayon-sy\">;<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>or<\/p>\n<div id=\"crayon-55752ae7c4954924921825\" class=\"crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate\" data-settings=\" minimize scroll-mouseover\">\n<div class=\"crayon-plain-wrap\"><textarea class=\"crayon-plain print-no\" readonly=\"readonly\" wrap=\"soft\" data-settings=\"dblclick\">document.getElementById(\u201cmyText\u201d).className = \u201canyclass\u201d;<\/textarea><\/div>\n<div class=\"crayon-main\">\n<table class=\"crayon-table\">\n<tbody>\n<tr class=\"crayon-row\">\n<td class=\"crayon-nums \" data-settings=\"show\">\n<div class=\"crayon-nums-content\">\n<div class=\"crayon-num\" data-line=\"crayon-55752ae7c4954924921825-1\">1<\/div>\n<\/div>\n<\/td>\n<td class=\"crayon-code\">\n<div class=\"crayon-pre\">\n<div id=\"crayon-55752ae7c4954924921825-1\" class=\"crayon-line\"><span class=\"crayon-v\">document<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-e\">getElementById<\/span><span class=\"crayon-sy\">(<\/span>\u201c<span class=\"crayon-i\">myText<\/span>\u201d<span class=\"crayon-sy\">)<\/span><span class=\"crayon-sy\">.<\/span><span class=\"crayon-v\">className<\/span> <span class=\"crayon-o\">=<\/span> \u201c<span class=\"crayon-i\">anyclass<\/span>\u201d<span class=\"crayon-sy\">;<\/span><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>1. What is JavaScript? JavaScript is a client-side scripting language that can be inserted into HTML pages and is understood by web browsers. 2. Enumerate the differences between Java and JavaScript? Java is a complete programming language. In contrast, JavaScript is a coded program that can be introduced to HTML pages. These two languages are [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4346,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[10],"tags":[51],"_links":{"self":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/4345"}],"collection":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/comments?post=4345"}],"version-history":[{"count":0,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/posts\/4345\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media\/4346"}],"wp:attachment":[{"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/media?parent=4345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/categories?post=4345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/a1webdesignteam.com\/blog\/wp-json\/wp\/v2\/tags?post=4345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}