Click⭐if you like the project and follow @SudheerJonna for more updates. Coding questions available . PDF and Epub versions available at actions tab. Show
Explore the best free resource to learn JavaScript. Build your own projects & earn a free certification in just 25 days.
JavaScript Interview Questions | Top JavaScript Interview Questions and Answers. Table of ContentsNo.Questions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
Coding Exercise1. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();39
Answer: 4The function declarations are hoisted similar to any variables. So the placement for class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");58 function declaration doesn't make any difference. 2. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();40
Answer: 3Of course the return value of class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");59 is 1 due to the increment operator. But the statement class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");60 declares a local variable x. Whereas y declared as a global variable accidentally. This statement is equivalent to, function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();41 Since the block scoped variable x is undefined outside of the function, the type will be undefined too. Whereas the global variable class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");61 is available outside the function, the value is 0 and type is number. 3. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();42
Answer: 4The statements order is based on the event loop mechanism. The order of statements follows the below order,
4. What is the output of below equality checkfunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();43
Answer: 1This is due to the float point math problem. Since the floating point numbers are encoded in binary format, the addition operations on them lead to rounding errors. Hence, the comparison of floating points doesn't give expected results. You can find more details about the explanation here 0.30000000000000004.com/ 5. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();44
Answer: 4The main points in the above code snippets are,
In other words, it is same as function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();45 Note: It returns 1object for MS Edge browser 6. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();46
Answer: 3This is a semicolon issue. Normally semicolons are optional in JavaScript. So if there are any statements(in this case, return) missing semicolon, it is automatically inserted immediately. Hence, the function returned as undefined. Whereas if the opening curly brace is along with the return keyword then the function is going to be returned as expected. function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();47 7. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();48
Answer: 3The class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");65 operator will delete the object property but it will not reindex the array or change its length. So the number or elements or length of the array won't be changed. If you try to print myChars then you can observe that it doesn't set an undefined value, rather the property is removed from the array. The newer versions of Chrome use class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");66 instead of class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");67 to make the difference a bit clearer. 8. What is the output of below code in latest Chromefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();49
Answer: 2The latest chrome versions display class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");68(they are filled with holes) using this empty x n notation. Whereas the older versions have undefined x n notation. Note: The latest version of FF displays class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");69 notation. 9. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();50
Answer: 1ES6 provides method definitions and property shorthands for objects. So both prop2 and prop3 are treated as regular function values. 10. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();51
Answer: 2The important point is that if the statement contains the same operators(e.g, < or >) then it can be evaluated from left to right. The first statement follows the below order,
Whereas the second statement follows the below order,
11. What is the output of below code in non-strict modefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();52
Answer: 2In non-strict mode, the regular JavaScript functions allow duplicate named parameters. The above code snippet has duplicate parameters on 1st and 3rd parameters. The value of the first parameter is mapped to the third argument which is passed to the function. Hence, the 3rd argument overrides the first parameter. Note: In strict mode, duplicate parameters will throw a Syntax Error. 12. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();53
Answer: 3Unlike regular functions, the arrow functions doesn't not allow duplicate parameters in either strict or non-strict mode. So you can see // Create a new instance using function prototype. var newInstance = Object.create(func.prototype) // Call the function var result = func.call(newInstance, x, y, z), // If the result is a non-null object then use it otherwise just use the new instance. console.log(result && typeof result === 'object' ? result : newInstance);32 in the console. 13. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();54
Answer: 1Arrow functions do not have an class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");73 bindings. So any reference to class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");74 variable tries to resolve to a binding in a lexically enclosing environment. In this case, the arguments variable is not defined outside of the arrow function. Hence, you will receive a reference error. Where as the normal function provides the number of arguments passed to the function function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();55 But If you still want to use an arrow function then rest operator on arguments provides the expected arguments function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();56 14. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();57
Answer: 2In order to be consistent with functions like class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");75, the standard method name for trimming the whitespaces is considered as class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");76. Due to web web compatibility reasons, the old method name 'trimLeft' still acts as an alias for 'trimStart'. Hence, the prototype for 'trimLeft' is always 'trimStart' 15. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();58
Answer: 4-Infinity is the initial comparant because almost every other value is bigger. So when no arguments are provided, -Infinity is going to be returned. Note: Zero number of arguments is a valid case. 16. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();59
Answer: 1As per the comparison algorithm in the ECMAScript specification(ECMA-262), the above expression converted into JS as below function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();60 So it doesn't matter about number brackets([]) around the number, it is always converted to a number in the expression. 17. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();61
Answer: 2The concatenation operator(+) is applicable for both number and string types. So if any operand is string type then both operands concatenated as strings. Whereas subtract(-) operator tries to convert the operands as number type. 18. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();62
Answer: 1In comparison operators, the expression class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");77 converted to Number([0].valueOf().toString()) which is resolved to false. Whereas class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");77 just becomes a truthy value without any conversion because there is no comparison operator. 19. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();63
Answer: 4The + operator is not meant or defined for arrays. So it converts arrays into strings and concatenates them. 20. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();64
Answer: 1Since // Create a new instance using function prototype. var newInstance = Object.create(func.prototype) // Call the function var result = func.call(newInstance, x, y, z), // If the result is a non-null object then use it otherwise just use the new instance. console.log(result && typeof result === 'object' ? result : newInstance);97 object is a collection of unique values, it won't allow duplicate values in the collection. At the same time, it is case sensitive data structure. 21. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();65
Answer: 2JavaScript follows IEEE 754 spec standards. As per this spec, NaNs are never equal for floating-point numbers. 22. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();66
Answer: 4The class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");08 uses strict equality operator(===) internally and class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");81 evaluates to false. Since indexOf won't be able to find NaN inside an array, it returns -1 always. But you can use class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");82 method to find out the index of NaN in an array or You can use class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");83 to check if NaN is present in an array or not. function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();67 23. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();68
Answer: 3When using rest parameters, trailing commas are not allowed and will throw a SyntaxError. If you remove the trailing comma then it displays 1st answer function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();69 25. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();70
Answer: 1Async functions always return a promise. But even if the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise. The above async function is equivalent to below expression, function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();71 26. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();72
Answer: 4The await expression returns value 10 with promise resolution and the code after each await expression can be treated as existing in a function func() {}; new func(x, y, z);59 callback. In this case, there is no return expression at the end of the function. Hence, the default return value of class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");67 is returned as the resolution of the promise. The above async function is equivalent to below expression, function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();73 27. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();74
Answer: 1Even though “processArray” is an async function, the anonymous function that we use for class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");86 is synchronous. If you use await inside a synchronous function then it throws a syntax error. 28. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();75
Answer: 4The forEach method will not wait until all items are finished but it just runs the tasks and goes next. Hence, the last statement is displayed first followed by a sequence of promise resolutions. But you control the array sequence using for..of loop, function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();76 29. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();77
Answer: 1Set has few exceptions from equality check,
30. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();78
Answer: 3Symbol follows below conventions,
Note: The symbol description is just useful for debugging purposes. 31. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();79
Answer: 1class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");88 is a just a standard function and not an object constructor(unlike other primitives new Boolean, new String and new Number). So if you try to call it with the new operator will result in a TypeError 32. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();80
Answer: 4The return value of class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");89 or class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");90 is always a truthy value (either "number" or "string"). The ! operator operates on either class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");89 or class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");90, converting them to boolean values. Since the value of both class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");93 and class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");94 is false, the if condition fails, and control goes to else block. To make the ! operator operate on the equality expression, one needs to add parentheses: function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();81 Or simply use the inequality operator: function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();82 33. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();83
Answer: 2The symbols has below constraints,
34. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();84
Answer: 2Using constructors, class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");35 refers to the constructor (points to the class definition of class which is initialized) that was directly invoked by new. This also applies to the case if the constructor is in a parent class and was delegated from a child constructor. 35. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();85
Answer: 4It throws a syntax error because the rest element should not have a trailing comma. You should always consider using a rest operator as the last element. 36. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();86
Answer: 1The object property follows below rules,
37. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();87
Answer: 2If you leave out the right-hand side assignment for the destructuring object, the function will look for at least one argument to be supplied when invoked. Otherwise you will receive an error class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");97 as mentioned above. You can avoid the error with either of the below changes,
function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();88
function Person() {} Person.prototype.name = "Sudheer"; var object = new Person();89 38. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();90
Answer: 1It is possible to combine Array and Object destructuring. In this case, the third element in the array props accessed first followed by name property in the object. 39. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();91
Answer: 3If the function argument is set implicitly(not passing argument) or explicitly to undefined, the value of the argument is the default parameter. Whereas for other falsy values('' or null), the value of the argument is passed as a parameter. Hence, the result of function calls categorized as below,
40. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();92
Answer: 2Since the default argument is evaluated at call time, a new object is created each time the function is called. So in this case, the new array is created and an element pushed to the default empty array. 41. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();93
Answer: 2Since parameters defined earlier are available to later default parameters, this code snippet doesn't throw any error. 42. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();94
Answer: 1The functions and variables declared in the function body cannot be referred from default value parameter initializers. If you still try to access, it throws a run-time ReferenceError(i.e, class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");98 is not defined). 43. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();95
Answer: 3The rest parameter is used to hold the remaining parameters of a function and it becomes an empty array if the argument is not provided. 44. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();96
Answer: 2Spread syntax can be applied only to iterable objects. By default, Objects are not iterable, but they become iterable when used in an Array, or with iterating functions such as class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");99. If you still try to do it, it still throws var object = new (function () { this.name = "Sudheer"; })();00. 45. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();97
Answer: 4Generators are not constructible type. But if you still proceed to do, there will be an error saying "TypeError: myGenFunc is not a constructor" 46. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();98
Answer: 1A return statement in a generator function will make the generator finish. If a value is returned, it will be set as the value property of the object and done property to true. When a generator is finished, subsequent next() calls return an object of this form: var object = new (function () { this.name = "Sudheer"; })();01. 47. What is the output of below codefunction Person() {} Person.prototype.name = "Sudheer"; var object = new Person();99
Answer: 4The generator should not be re-used once the iterator is closed. i.e, Upon exiting a loop(on completion or using break & return), the generator is closed and trying to iterate over it again does not yield any more results. Hence, the second loop doesn't print any value. 48. What is the output of below codefunction func() {}; new func(x, y, z);00
Answer: 1If you use an invalid number(outside of 0-7 range) in the octal literal, JavaScript will throw a SyntaxError. In ES5, it treats the octal literal as a decimal number. 49. What is the output of below codefunction func() {}; new func(x, y, z);01
Answer: 2Unlike function declarations, class declarations are not hoisted. i.e, First You need to declare your class and then access it, otherwise it will throw a ReferenceError "Uncaught ReferenceError: Square is not defined". Note: Class expressions also applies to the same hoisting restrictions of class declarations. 50. What is the output of below codefunction func() {}; new func(x, y, z);02
Answer: 4When a regular or prototype method is called without a value for this, the methods return an initial this value if the value is not undefined. Otherwise global window object will be returned. In our case, the initial function func() {}; new func(x, y, z);35 value is undefined so both methods return window objects. 51. What is the output of below codefunction func() {}; new func(x, y, z);03
Answer: 3The super keyword is used to call methods of a superclass. Unlike other languages the super invocation doesn't need to be a first statement. i.e, The statements will be executed in the same order of code. 52. What is the output of below codefunction func() {}; new func(x, y, z);04
Answer: 2Even though we used constant variables, the content of it is an object and the object's contents (e.g properties) can be altered. Hence, the change is going to be valid in this case. 53. What is the output of below codefunction func() {}; new func(x, y, z);05
Answer: 2Emojis are unicodes and the unicode for smile symbol is "U+1F642". The unicode comparision of same emojies is equivalent to string comparison. Hence, the output is always true. 54. What is the output of below code?function func() {}; new func(x, y, z);06
Answer: 1The typeof operator on any primitive returns a string value. So even if you apply the chain of typeof operators on the return value, it is always string. 55. What is the output of below code?function func() {}; new func(x, y, z);07
Answer: 1
Hence the above code block always goes to if section. 55. What is the output of below code in non strict mode?function func() {}; new func(x, y, z);08
Answer: 4It returns undefined for non-strict mode and returns Error for strict mode. In non-strict mode, the wrapper object is going to be created and get the mentioned property. But the object get disappeared after accessing the property in next line. 56. What is the output of below code?function func() {}; new func(x, y, z);09
Answer: 111 and 10 is logged to the console. The innerFunc is a closure which captures the count variable from the outerscope. i.e, 10. But the conditional has another local variable var object = new (function () { this.name = "Sudheer"; })();03 which overwrites the ourter var object = new (function () { this.name = "Sudheer"; })();03 variable. So the first console.log displays value 11. Whereas the second console.log logs 10 by capturing the count variable from outerscope. 57. What is the output of below code ?
Reason : The operator returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy. Note: Below these values are consider as falsy value
58. What is the output of below code ?function func() {}; new func(x, y, z);10
Answer: 3Arrays have their own implementation of var object = new (function () { this.name = "Sudheer"; })();05 method that returns a comma-separated list of elements. So the above code snippet returns true. In order to avoid conversion of array type, we should use === for comparison. 59. What is the output of below code?function func() {}; new func(x, y, z);11
Answer: 2Hoisting will move variables and functions to be the top of scope. Even though getMessage is an arrow function the above function will considered as a varible due to it's variable declaration or assignment. So the variables will have undefined value in memory phase and throws an error ' var object = new (function () { this.name = "Sudheer"; })();06 is not a function' at the code execution phase. 60. What is the output of below code?function func() {}; new func(x, y, z);12
Answer: 3Even though a promise is resolved immediately, it won't be executed immediately because its .then/catch/finally handlers or callbacks(aka task) are pushed into the queue. Whenever the JavaScript engine becomes free from the current program, it pulls a task from the queue and executes it. This is the reason why last statement is printed first before the log of promise handler. Note: We call the above queue as "MicroTask Queue" 61. What is the output of below code?function func() {}; new func(x, y, z);13
Answer: 4When JavaScript encounters a line break without a semicolon, the JavaScript parser will automatically add a semicolon based on a set of rules called var object = new (function () { this.name = "Sudheer"; })();13 which determines whether line break as end of statement or not to insert semicolon. But it does not assume a semicolon before square brackets [...]. So the first two lines considered as a single statement as below. function func() {}; new func(x, y, z);14 Hence, there will be cannot read properties of undefined error while applying the array square bracket on log function. 62. Write a function that returns a random HEX colorSolution 1 (Iterative generation)function func() {}; new func(x, y, z);15Solution 2 (One-liner) function func() {}; new func(x, y, z);16 63. What is the output of below code?function func() {}; new func(x, y, z);17
Answer: 1In JavaScript, var object = new (function () { this.name = "Sudheer"; })();14 is not considered as a reserved keyword. So the variable declaration with var object = new (function () { this.name = "Sudheer"; })();14 is accepted and prints the array value var object = new (function () { this.name = "Sudheer"; })();14 using for..of loop. But if you use reserved keyword such as var object = new (function () { this.name = "Sudheer"; })();17 then there will be a syntax error saying var object = new (function () { this.name = "Sudheer"; })();18, function func() {}; new func(x, y, z);18 64. What is the output of below code?function func() {}; new func(x, y, z);19
Answer: 2By default, the sort method sorts elements alphabetically. This is because elemented converted to strings and strings compared in UTF-16 code units order. Hence, you will see the above numbers not sorted as expected. In order to sort numerically just supply a comparator function which handles numeric sorts. function func() {}; new func(x, y, z);20 Note: Sort() method changes the original array. 65. What is the output order of below code?function func() {}; new func(x, y, z);21
Answer: 466. What is the output of below code?function func() {}; new func(x, y, z);22
Answer: 4IIFE(Immediately Invoked Function Expression) is just like any other function expression which won't be hoisted. Hence, there will be a reference error for message call. The behavior would be the same with below function expression of message1, function func() {}; new func(x, y, z);23 67. What is the output of below code?function func() {}; new func(x, y, z);24
Answer: 3As part of hoisting, initially JavaScript Engine or compiler will store first function in heap memory but later rewrite or replaces with redefined function content. 68. What is the output of below code?function func() {}; new func(x, y, z);25
Answer: 3Due to hositing feature, the variables declared with function func() {}; new func(x, y, z);40 will have class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");67 value in the creation phase so the outer variable var object = new (function () { this.name = "Sudheer"; })();21 will get same class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");67 value. But after few lines of code JavaScript engine found a new function call( var object = new (function () { this.name = "Sudheer"; })();23) to update the current city with function func() {}; new func(x, y, z);40 re-declaration. Since each function call will create a new execution context, the same variable will have class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");67 value before the declaration and new value( var object = new (function () { this.name = "Sudheer"; })();26) after the declarion. Hence, the value class Person { constructor(name) { this.name = name; } } var object = new Person("Sudheer");67 print first followed by new value var object = new (function () { this.name = "Sudheer"; })();26 in the execution phase. 69. What is the output of below code in an order?function func() {}; new func(x, y, z);26
Answer: 1Each context(global or functional) has it's own variable environment and the callstack of variables in a LIFO order. So you can see the message variable value from second, first functions in an order followed by global context message variable value at the end. 70. What is the output of below code?function func() {}; new func(x, y, z);27
Answer: 1The function call var object = new (function () { this.name = "Sudheer"; })();29 is not going to be part of scope chain and it has it's own execution context with the enclosed variable environment. i.e, It won't be accessed from global context. Hence, there will be an error while invoking the function as var object = new (function () { this.name = "Sudheer"; })();30. 71. What is the output of below code?function func() {}; new func(x, y, z);28
Answer: 4function func() {}; new func(x, y, z);35 keyword is dynamic scoped but not lexically scoped . In other words, it doesn't matter where function func() {}; new func(x, y, z);35 has been written but how it has been invoked really matter. In the above code snippet, the var object = new (function () { this.name = "Sudheer"; })();33 object invokes var object = new (function () { this.name = "Sudheer"; })();34 function so function func() {}; new func(x, y, z);35 keyword refers to var object = new (function () { this.name = "Sudheer"; })();33 object but var object = new (function () { this.name = "Sudheer"; })();37 has been invoked by var object = new (function () { this.name = "Sudheer"; })();34 function and function func() {}; new func(x, y, z);35 will have default var object = new (function () { this.name = "Sudheer"; })();40 object. The above pit fall fixed by three ways,
function func() {}; new func(x, y, z);29 The next two solutions have been used before ES6 introduced.
function func() {}; new func(x, y, z);30
function func() {}; new func(x, y, z);31 72. What is the output of below code?function func() {}; new func(x, y, z);32
Answer: 3In JavaScript, primitives are immutable i.e. there is no way to change a primitive value once it gets created. So when you try to update the string's first character, there is no change in the string value and prints the same initial value var object = new (function () { this.name = "Sudheer"; })();53. Whereas in the later example, the concatenated value is re-assigned to the same variable which will result into creation of new memory block with the reference pointing to var object = new (function () { this.name = "Sudheer"; })();54 value and the old memory block value( var object = new (function () { this.name = "Sudheer"; })();55) will be garbage collected. 73. What is the output of below code?function func() {}; new func(x, y, z);33
Answer: 2In JavaScript, the variables such as objects, arrays and functions comes under pass by reference. When you try to compare two objects with same content, it is going to compare memory address or reference of those variables. These variables always create separate memory blocks hence the comparison is always going to return false value. 74. What is the output of below code?function func() {}; new func(x, y, z);34
Answer: 3The variable var object = new (function () { this.name = "Sudheer"; })();56 is still treated as closure(since it has been used in inner function) eventhough it has been declared after setTimeout function. The function with in setTimeout function will be sent to WebAPI and the variable declaration executed with in 5 seconds with the assigned value. Hence, the text declared for the variable will be displayed. DisclaimerThe questions provided in this repository are the summary of frequently asked questions across numerous companies. We cannot guarantee that these questions will actually be asked during your interview process, nor should you focus on memorizing all of them. The primary purpose is for you to get a sense of what some companies might ask — do not get discouraged if you don't know the answer to all of them — that is ok! |