

{"id":68123,"date":"2019-08-17T16:57:58","date_gmt":"2019-08-17T11:27:58","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=68123"},"modified":"2019-08-17T17:11:23","modified_gmt":"2019-08-17T11:41:23","slug":"javascript-interview-questions-and-answers","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/","title":{"rendered":"Advanced JavaScript Interview Questions and Answers &#8211; Explore now &amp; Lead tomorrow!"},"content":{"rendered":"<div class='__iawmlf-post-loop-links' style='display:none;' data-iawmlf-post-links='[{&quot;id&quot;:1442,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Access_modifiers&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250914021739\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Access_modifiers&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 07:03:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-05 08:40:07&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-21 12:59:55&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-11 18:32:29&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-20 08:48:46&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-03 11:17:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-13 18:47:53&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-28 16:48:01&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-05-28 16:48:01&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]'><\/div>\n<p>The first part of DataFlair&#8217;s <a href=\"https:\/\/data-flair.training\/blogs\/javascript-interview-questions\/\"><em><strong>JavaScript Interview Questions<\/strong> <\/em><\/a>was more inclined towards the testing of your theoretical knowledge of JavaScript. This article on JavaScript interview questions and answers follows a practical approach towards the language and will play a major part in cracking your JavaScript interview.<\/p>\n<p>After completing all the topics of <a href=\"https:\/\/data-flair.training\/blogs\/javascript-tutorials-home\/\"><em><strong>JavaScript tutorial series<\/strong><\/em><\/a>, these JavaScript interview questions and answers should not be much trouble for you. And, I assure you that after practicing this set of JavaScript interview questions and answers, you will definitely feel more relaxed in your interview.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-68161\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg\" alt=\"JavaScript Interview Questions and Answers for Experienced Professionals\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>Top JavaScript Interview Questions and Answers<\/h2>\n<p>Here are some of the lastest JavaScript interview questions for experienced professionals. Practice them and get your dream job.<\/p>\n<p><strong>Q 1. Is it possible to write a multi-line string in JavaScript?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Yes, JavaScript provides the facility to write a String in multiple lines using the following three approaches:<\/p>\n<ul>\n<li><strong>Using backticks<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var string = `line1\r\nline2\r\nline3`;<\/pre>\n<ul>\n<li><strong>Using + operator<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var string = \u201cline1\u201d +\r\n\u201cline2\u201d +\r\n\u201cline3\u201d;<\/pre>\n<ul>\n<li><strong>Using \\ (backslash)<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var string = \u201cline1 \\\r\nline2 \\\r\nline3\u201d;<\/pre>\n<p><em><strong>Grab the complete knowledge of <a href=\"https:\/\/data-flair.training\/blogs\/javascript-strings\/\">JavaScript Strings Methods<\/a><\/strong><\/em><\/p>\n<p><strong>Q 2. Discuss a way to convert the string of any base to integer in JavaScript?<\/strong><\/p>\n<p><strong>Ans.<\/strong> We can convert a string of any base to the integer if we wish with the help of the JavaScript built-in method called parseInt(). This method has two parameters: string and radix. The second parameter is optional and used when you want to get the integer equivalent to the radix as the base. The default value is 10 for decimals values.<\/p>\n<p style=\"text-align: center\"><em><strong>parseInt(\u201c9d\u201d, 16) \/\/ 157<\/strong><\/em><\/p>\n<p><strong>Q 3. While creating a webpage, a programmer wanted to print the output \u201cThis is DataFlair\u2019s JavaScript tutorial\u201d. Instead, he encounters a SyntaxError in the browser console. What went wrong, and can he fix it? He used the following statement:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var string = 'This is DataFlair's JavaScript tutorial'<\/pre>\n<p><strong>Ans.<\/strong> The programmer wrote confusing statements for the JavaScript interpreter. He has two options to resolve this error:<\/p>\n<ul>\n<li><strong>Using escape characters &#8211;<\/strong> He can use \\\u2019 instead of \u2018 after DataFlair. This will inform the script that the inverted comma is part of the string and the string is going to continue.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var string = 'This is DataFlair\\'s JavaScript tutorial'<\/pre>\n<ul>\n<li><strong>Using double inverted commas &#8211;<\/strong> The programmer can use double inverted commas to wrap the string rather than the single inverted comma.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var string = 'This is DataFlair's JavaScript tutorial'<\/pre>\n<p><em><strong>Time to learn everything about <a href=\"https:\/\/data-flair.training\/blogs\/javascript-characters\/\">JavaScript Characters<\/a><\/strong><\/em><\/p>\n<p><strong>Q 4. What are the different methods to empty a JavaScript Array?<\/strong><\/p>\n<p><strong>Ans.<\/strong> You can use the following techniques to produce an empty array:<\/p>\n<ul>\n<li>You can redefine the older array as an empty array.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">emptyArray = [ ];<\/pre>\n<ul>\n<li>You can set the length of a predefined array to zero.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">definedArray.length = 0;<\/pre>\n<ul>\n<li>You can update the array to contain zero elements with the help of the JavaScript splice() method.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">definedArray.splice(0, definedArray.length);<\/pre>\n<ul>\n<li>You can remove all the elements of the array using array methods: pop() or shift().<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">while(definedArray.length){\r\n        definedArray.pop();\r\n} \/\/pop() method<\/pre>\n<p>Or<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">while(definedArray.length){\r\n        definedArray.shift();\r\n} \/\/shift() method<\/pre>\n<p><em><strong>Don&#8217;t forget to check &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/javascript-array\/\">JavaScript Array<\/a><\/strong><\/em><\/p>\n<p><strong>Q 5. Determine the output of the following code.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var courses = [\"JavaScript\",\"Java\",\"C\",\"C++\",\"Python\"];\r\ndelete courses[2];\r\nconsole.log(courses.length);<\/pre>\n<p><strong>Ans.<\/strong> The output of the code is <strong>5<\/strong>. This is because the delete operator does not affect the length of the array as the operator deletes only the value present at the position. At the deleted index, we get the value undefinedx1 (in Chrome) or just undefined\/ empty. Thus, if we tried the statement <strong>console.log(courses)<\/strong>, we will get <strong>(5) [&#8220;JavaScript&#8221;, &#8220;Java&#8221;, empty, &#8220;C++&#8221;, &#8220;Python&#8221;]<\/strong>.<\/p>\n<p><strong>Q 6. Explain the role of closures in JavaScript.<\/strong><\/p>\n<p><strong>Ans.<\/strong> In JavaScript, the script creates a closure at the time of function creation. It is a local variable that stays in the memory even after the function completes its execution. It has access to the variables in three scopes: variable in its own scope, variables in the enclosing function\u2019s scope, and global variables. Closures are crucial in JavaScript to ensure the privacy of variables, that is, to create private variables. Since JavaScript has no <a href=\"https:\/\/en.wikipedia.org\/wiki\/Access_modifiers\">access modifiers<\/a>, closures allow the programmer to create variables that are not directly accessible.<\/p>\n<p><em><strong>Master the concept of <a href=\"https:\/\/data-flair.training\/blogs\/javascript-closures\/\">JavaScript Closures<\/a>\u00a0and its implementation<\/strong><\/em><\/p>\n<p><strong>Q 7. Consider the code snippet below. What will the console output be and why?<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">(function(x) {\r\nreturn (function(y) {\r\n    console.log(x);\r\n})(2)\r\n})(1);<\/pre>\n<p><strong>Ans.<\/strong> The output of the function is 1, even though we never set the value of x in the inner function. The reason behind this is that the above function is a closure. Thus, the inner function has access to the outer function\u2019s variables. In the example code, since the interpreter didn\u2019t find the value of x in the inner function, it searched for its defined value in the outside function. That\u2019s where it found x to have a value of 1.<\/p>\n<p><strong>Q 8. Is it possible to remove the content from a website? If yes, how?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Yes, there are a few possible ways to remove content from the website at will.<br \/>\nThe easiest way is to hide the content from the browser you want to remove. The <strong>display<\/strong> property of the <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/javascript-style-attribute\/\">JavaScript style attribute<\/a> <\/strong><\/em>helps us achieve the task. Whenever we add any content to the webpage, its default display sets to block (<strong>display: block<\/strong>). We can change the value to none (<strong>display: none<\/strong>). This will prevent the content from showing in the browser window. This approach only hides the details, it will still be available in the source code, and you can access it from the Elements window.<\/p>\n<p>Another way to do so is by removing the element node corresponding to the content. This technique completely removes the data from the webpage as well as the code. We can do this by <strong>.removeChild()<\/strong> method. Or if we are dealing with an array (including a NodeList), then we can easily empty the array, and get the same result.<\/p>\n<p>The decision of selecting the appropriate method for the situation depends on the programmer and his\/ her needs.<\/p>\n<p><strong>Q 9. Write the JavaScript code to print the following output on your console window.<\/strong><\/p>\n<p style=\"text-align: center\"><em><strong>Array: [\u201cDataFlair\u201d, 2019, 1.0, true]<\/strong><\/em><\/p>\n<p><strong>Ans.<\/strong> The code to print the following output in the console window is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n  &lt;body&gt;\r\n\r\n    &lt;script&gt;\r\n        var array = [\"DataFlair\", 2019, 1.0, true];\r\n        var msg = \"Array: [\";\r\n        for(var i = 0; i &lt; array.length-1; i++){\r\n            msg += array[i] + \", \";\r\n        }\r\n        msg += array[array.length-1] + \"]\";\r\n            console.log(msg);\r\n    &lt;\/script&gt;\r\n\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Q 10. What are the possible ways to write a function isInteger(value), that determines if the value is an integer?<\/strong><\/p>\n<p><strong>Ans.<\/strong> EcmaScript 6 introduced a new function, Number.isInteger(), to determine whether a value is an integer or not. But the specifications before EcmaScript 6 have no such function. In fact, there are no integers; numeric values are always in the form of floating-point values.<\/p>\n<p>The following are some of the functions you can declare to check for an integer pre-EcmaScript-6:<\/p>\n<ul>\n<li><strong>Using the bitwise operator:<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function isInteger(value) { return (value ^ 0) === value; }<\/pre>\n<ul>\n<li><strong>Using the Math object:<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function isInteger(value) { return Math.round(value) === value; }<\/pre>\n<p>In this approach, Math.ceil() and Math.floor() methods are equally capable to implement the function.<\/p>\n<ul>\n<li><strong>Using the typeof and strict equality operator:<\/strong><\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function isInteger(value) { return (typeof value === 'number') &amp;&amp; (value % 1 === 0); }<\/pre>\n<p>There is another way for the problem, but it only works with small numeric values.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function isInteger(value) { return parseInt(value, 10) === value; }<\/pre>\n<p>The problem with this process: it fails when the value of \u201cvalue\u201d becomes quite large. It is because <strong>parseInt()<\/strong> converts the first parameter to a string before parsing the digits. Thus, with a large value, the string will be represented in exponential form (e.g., 1e30). Once parseInt() reaches the character \u2018e\u2019, it stops parsing, and thus returns the value 1 (in this case). So, this method is fine as long as you only need to work with small numbers. Otherwise, you may want to consider the above techniques.<\/p>\n<p><em><strong>Get to learn the different <a href=\"https:\/\/data-flair.training\/blogs\/javascript-numbers\/\">methods to write a number in JavaScript<\/a><\/strong><\/em><\/p>\n<p><strong>Q 11. Write a program to calculate the length of an associative array.<\/strong><\/p>\n<p><strong>Ans.<\/strong> The JavaScript code to count the length of an associative array is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n  &lt;body&gt;\r\n    &lt;script&gt;\r\n      \/\/an associative array\r\n      var associativeArray = {one: \"DataFlair\", two:\"JavaScript\", three: 435, four: true};\r\n      var count = 0;\r\n      \/\/calculating the length of the array\r\n      for (arrayValue in associativeArray){\r\n        count = count + 1;\r\n      }\r\n      console.log(count);\r\n    &lt;\/script&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>The code produces the output 4 in the browser console.<\/p>\n<p>Any doubts in the JavaScript interview questions and answers till now? Share them in the comment section.<\/p>\n<p><strong>Q 12. What is a possible dilemma with <em>typeof obj === &#8220;object&#8221;<\/em> to determine if <em>obj<\/em> is an object? How can we avoid this?<\/strong><\/p>\n<p><strong>Ans.<\/strong> The above statement is a reliable way of checking if <strong>obj<\/strong> is an object, but in JavaScript, <strong>null<\/strong> is also an object. Thus the following statements will return true in the console, surprising most of the developers:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">var obj = null;\r\nconsole.log(typeof obj === \"object\");<\/pre>\n<p>As long as you keep this problem in mind, you can easily use the typeof operator to determine whether <strong>obj<\/strong> is an object or not. Remember, this method will return <strong>false<\/strong> if <strong>obj<\/strong> is a function, <strong>true<\/strong> if <strong>obj<\/strong> is an array.<\/p>\n<p>An alternative condition that returns <strong>false<\/strong> if <strong>obj<\/strong> is null, array, or function, and returns <strong>true<\/strong> for objects is this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">console.log((obj !== null) &amp;&amp; (obj.constructor === Object));<\/pre>\n<p><em><strong>Must Learn &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/javascript-objects\/\">How to Create JavaScript Objects<\/a><\/strong><\/em><\/p>\n<p><strong>Q 13. What would the following code return?<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">console.log(typeof typeof 1);<\/pre>\n<p>The output of the code above in the console will be \u2018string\u2019. The value returned by typeof 1 is \u2018number\u2019; typeof \u2018number\u2019 will return a string.<\/p>\n<p><strong>Q 14. Write a simple function, indicating whether or not a string is a palindrome.<\/strong><\/p>\n<p>The following code checks whether the two strings are palindromes or not.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n  &lt;body&gt;\r\n\r\n    &lt;script&gt;\r\n        function isPalindrome(str) {\r\n                str = str.replace(\/\\W\/g, '').toLowerCase(); \/\/gets a lowercase string\r\n                var string = str.split('').reverse().join(''); \/\/reverses the string\r\n                \/\/checks if the string and the reversed strings are the same\r\n                if(string == str)\r\n                        return \"A palindrome\"\r\n                else\r\n                        return \"Not a palindrome\"\r\n        }\r\n        console.log(isPalindrome(\"level\"));\r\n        console.log(isPalindrome(\"levels\"));\r\n    &lt;\/script&gt;\r\n\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>The output in the browser console shows the following output:<\/p>\n<p>A palindrome<br \/>\nNot a palindrome<\/p>\n<p><strong>Q 15. Explain the process of document loading.<\/strong><\/p>\n<p><strong>Ans.<\/strong> Loading a document means getting it ready for execution on the system. When there is a running document on the system, the document loads on the browser. The application (browser) allows the JavaScript engine to do two tasks:<\/p>\n<ul>\n<li>Look for all the properties, given to the object.<\/li>\n<li>Include all the property values, used in the content that is being rendered for the page about to load.<\/li>\n<\/ul>\n<p>To load the document immediately, it is good practice to add the &lt;script&gt; tag inside the &lt;body&gt; tag. The following program loads the document immediately and returns the OS details of the user:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;html&gt;\r\n  &lt;body&gt;\r\n  &lt;H1&gt;JavaScript used&lt;\/H1&gt;\r\n\r\n    &lt;script&gt;\r\n        &lt;!-- Comments to hide js in old browsers\r\n        document.write(\"Give the version: \" + navigator.appVersion)\r\n        document.write(\" of &lt;b&gt;\" + navigator.appName + \"&lt;\/b&gt;.\")\r\n        \/\/ end of the comment section --&gt;\r\n    &lt;\/script&gt;\r\n\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p><strong>Q 16. Consider the following code. What will the output be, and why?<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">(function () {\r\n    try {\r\n      throw new Error();\r\n    } catch (x) {\r\n      var x = 1, y = 2;\r\n      console.log(x);\r\n    }\r\n    console.log(x);\r\n    console.log(y);\r\n})();<\/pre>\n<p><strong>Ans.<\/strong><\/p>\n<p>The output of the code in the console is as follows:<br \/>\n1<br \/>\nundefined<br \/>\n2<\/p>\n<p>In JavaScript, all the var statements are \u2018hoisted\u2019 (without their value initialization) to the top of the global\/ function scope it belongs to, even inside a try-catch block. Though, the error\u2019s identifier is only visible inside the catch block. The equivalent code looks like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">(function () {\r\n    var x, y; \/\/ outer variables, hoisted\r\n    try {\r\n        throw new Error();\r\n    } catch (x \/* inner x *\/) {\r\n        x = 1; \/\/ inner x, not the outer one\r\n        y = 2; \/\/ there is only one y (in the outer scope)\r\n        console.log(x \/* inner x *\/);\r\n    }\r\n    console.log(x);\r\n    console.log(y);\r\n})();\r\n\r\n<\/pre>\n<p><em><strong>Explore the concept of JavaScript Function Scope and different <a href=\"https:\/\/data-flair.training\/blogs\/javascript-function\/\">types of JavaScript Functions<\/a><\/strong><\/em><\/p>\n<p><strong>Q 17. Consider the following code snippet:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">for (var i = 0; i &lt; 5; i++) {\r\n  var btn = document.createElement('button');\r\n  btn.appendChild(document.createTextNode('Button ' + i));\r\n  btn.addEventListener('click', function(){ console.log(i); });\r\n  document.body.appendChild(btn);\r\n}<\/pre>\n<p><strong>What is the output in the console when the user clicks on \u201cButton 4\u201d and why?<\/strong><\/p>\n<p>No matter what button the user clicks, the output will always be 5. This is because when the onclick event invokes (when the user clicks any of the buttons), the \u2018for\u2019 loop is already finished and the value of the variable i is set to 5. All this happens at the time the document loads; the onclick event occurs later.<\/p>\n<p><strong>Q 18. When should one use Arrow functions in ES6?<\/strong><\/p>\n<p><strong>Ans.<\/strong> The thumb rule for functions in ES6 and beyond is as follows:<\/p>\n<ul>\n<li>Use functions in the global scope and for Object.prototype properties.<\/li>\n<li>Use class for object constructors.<\/li>\n<li>Use =&gt; everywhere else.<\/li>\n<\/ul>\n<p>The reason for using arrow functions almost everywhere is due to the following reasons:<\/p>\n<ul>\n<li><strong>Scope safety &#8211;<\/strong> Consistent use of arrow functions guarantees to use the same thisObject as the root. If even a single standard function callback gets mixed-up in with a bunch of arrow functions, there&#8217;s a chance the scope will become confusing.<\/li>\n<li><strong>Compactness &#8211;<\/strong> Compared to regular functions, arrow functions are easier to read and write. They also reduce the length of the JavaScript code.<\/li>\n<li><strong>Clarity &#8211;<\/strong> With almost everything defined in an arrow function, a regular function is extremely noticeable. You can always look up the next-higher function statement to see what the thisObject (root object) is.<\/li>\n<\/ul>\n<p><strong>Q 19. What are IIFEs?<\/strong><\/p>\n<p><strong>Ans.<\/strong> IIFEs (Immediately Invoked Function Expressions), also called Self-Executing Anonymous Functions, are functions that execute immediately after their definition. These are also a design pattern that contains two major parts:<\/p>\n<ul>\n<li>The first part is an anonymous function with a lexical scope enclosed within the Grouping operator (). This restricts the access of variables within the IIFE idiom and prevents it from polluting the global scope.<\/li>\n<li>The second part of IIFE creates the immediately executing function expression, through which the JavaScript engine will directly interpret the function.<\/li>\n<\/ul>\n<p>The syntax of an IIFE is as follows:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">(function () {\r\n    statements\r\n})();<\/pre>\n<p>All the variables defined inside an IIFE are not visible to the outside scope.<\/p>\n<p><em><strong>Let&#8217;s revise the <a href=\"https:\/\/data-flair.training\/blogs\/javascript-variable-tutorial\/\">concept of JavaScript Variables<\/a><\/strong><\/em><\/p>\n<p><strong>Q 20. How can you remove duplicate values from a JavaScript array?<\/strong><\/p>\n<p><strong>Ans.<\/strong> There are multiple ways to remove duplicate values from an array; the most common ones are as follows:<\/p>\n<ul>\n<li><strong>By using set<\/strong><\/li>\n<\/ul>\n<p>This is the easiest way to remove duplicate values from an array. Set is a built-in JavaScript object that stores the unique values in another array. The following program will create an array with unique values:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function uniqueArray(array) {\r\n        var newSet= new Set(array);\r\n            var newArray = Array.from(newSet);\r\n        return newArray;\r\n}\r\nvar arr = [1,5,2,4,1,6]\r\nconsole.log(uniqueArray(arr));<\/pre>\n<ul>\n<li><strong>By using filter<\/strong><\/li>\n<\/ul>\n<p>This approach removes all the duplicate values from the array by applying a filter on it. The filter() method requires three arguments: element (we want to check for duplicity), index (of the element) and array (containing duplicates). The following program returns a unique array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function uniqueArray(arr){\r\n        var newSet = arr.filter(function(elem, index, self) {\r\n            return index == self.indexOf(elem);\r\n        })\r\n        return newSet;\r\n}\r\nvar arr = [1,5,2,4,1,6]\r\nconsole.log(uniqueArray(arr));<\/pre>\n<ul>\n<li><strong>By using for loop<\/strong><\/li>\n<\/ul>\n<p>This method is a little tiresome and not always recommended. But you can create a unique array by creating an empty array and only adding new values that are not already present in the array. The code implementation is something like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">function uniqueArray(dups_names) {\r\n    var unique = [];\r\n    arr.forEach(function(i) {\r\n        if (!unique[i]) {\r\n                unique[i] = true;\r\n        }\r\n    });\r\n    return Object.keys(unique);\r\n}\r\nvar arr = [1,5,2,4,1,6]\r\nconsole.log(uniqueArray(arr));<\/pre>\n<h2>Summary<\/h2>\n<p>This was all in the tricky JavaScript interview questions and answers. We tried to cover all the core JavaScript technical interview questions which are mostly asked in a JavaScript interview.<\/p>\n<p>I hope you are clear with all the JavaScript interview questions and answers. If you have come across with queries regarding any JavaScript interview question or answer, mention them in the comment section.<\/p>\n<p><em><strong>Next article in the JavaScript DataFlair Tutorial Series for you &#8211; <a href=\"https:\/\/data-flair.training\/blogs\/javascript-project-photo-gallery\/\">JavaScript Project on Photo Gallery<\/a><\/strong><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first part of DataFlair&#8217;s JavaScript Interview Questions was more inclined towards the testing of your theoretical knowledge of JavaScript. This article on JavaScript interview questions and answers follows a practical approach towards the&#46;&#46;&#46;<\/p>\n","protected":false},"author":7,"featured_media":68161,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18979],"tags":[20893,20871,20895,20894,19297],"class_list":["post-68123","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-advanced-javascript-interview-questions","tag-javascript-interview-questions-and-answers","tag-javascript-practical-interview-questions","tag-javascript-technical-interview-questions","tag-top-javascript-interview-questions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Advanced JavaScript Interview Questions and Answers - Explore now &amp; Lead tomorrow! - DataFlair<\/title>\n<meta name=\"description\" content=\"JavaScript interview questions and answers to get a thorough understanding of JavaScript language and crack the interview in first attempt.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Advanced JavaScript Interview Questions and Answers - Explore now &amp; Lead tomorrow! - DataFlair\" \/>\n<meta property=\"og:description\" content=\"JavaScript interview questions and answers to get a thorough understanding of JavaScript language and crack the interview in first attempt.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-17T11:27:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-08-17T11:41:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Advanced JavaScript Interview Questions and Answers - Explore now &amp; Lead tomorrow! - DataFlair","description":"JavaScript interview questions and answers to get a thorough understanding of JavaScript language and crack the interview in first attempt.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/","og_locale":"en_US","og_type":"article","og_title":"Advanced JavaScript Interview Questions and Answers - Explore now &amp; Lead tomorrow! - DataFlair","og_description":"JavaScript interview questions and answers to get a thorough understanding of JavaScript language and crack the interview in first attempt.","og_url":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-08-17T11:27:58+00:00","article_modified_time":"2019-08-17T11:41:23+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd"},"headline":"Advanced JavaScript Interview Questions and Answers &#8211; Explore now &amp; Lead tomorrow!","datePublished":"2019-08-17T11:27:58+00:00","dateModified":"2019-08-17T11:41:23+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/"},"wordCount":2228,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg","keywords":["Advanced JavaScript Interview Questions","JavaScript Interview Questions and Answers","JavaScript Practical Interview Questions","JavaScript technical interview questions","Top JavaScript Interview Questions"],"articleSection":["JavaScript Tutorial"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/","url":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/","name":"Advanced JavaScript Interview Questions and Answers - Explore now &amp; Lead tomorrow! - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg","datePublished":"2019-08-17T11:27:58+00:00","dateModified":"2019-08-17T11:41:23+00:00","description":"JavaScript interview questions and answers to get a thorough understanding of JavaScript language and crack the interview in first attempt.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/08\/JavaScript-Interview-Questions-and-Answers.jpg","width":802,"height":420,"caption":"JavaScript Interview Questions and Answers for Experienced Professionals"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/javascript-interview-questions-and-answers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"JavaScript Tutorial","item":"https:\/\/data-flair.training\/blogs\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Advanced JavaScript Interview Questions and Answers &#8211; Explore now &amp; Lead tomorrow!"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/beb0cab24b7aa54423a3b50e669a9dcd","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322416204232f4dd97ef3901b0a499a5d34d7ba7fe333f4bfe53a907873d293?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team specializes in creating clear, actionable content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Backed by industry expertise, we make learning easy and career-oriented for beginners and pros alike.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam3\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=68123"}],"version-history":[{"count":6,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68123\/revisions"}],"predecessor-version":[{"id":68163,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/68123\/revisions\/68163"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/68161"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=68123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=68123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=68123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}