JavaScript Interview Coding Challenges
JavaScript Interview Coding Challenges
IIFE, seen in "Code 3" and "Code 1" of Source 1, creates an isolated scope, allowing encapsulation of variables. Inside the IIFE, variables maintain local scope, preventing them from affecting or being affected by the global scope. This leads to temporal dead zone issues if variables are accessed before declaration, requiring careful order of operations inside IIFEs .
Closures trap variables rather than their values, which in the context of "Code 2" in Source 1 results in the loop index `i` being common across iterations. Hence, it logs the last value of `i` (3) multiple times. To correct such behavior, closures can be utilized by capturing `i` inside another function or using `let` instead of `var` to maintain block scope .
JavaScript performs implicit type conversion in expressions. In "Code 2" from Source 1, "1" + 1 results in "11" due to string concatenation, while "A" - 1 results in NaN because 'A' cannot be converted to a number. This demonstrates automatic type conversions in mixed-type operations, often leading to unexpected results .
In nested functions seen in "Code 3" from Source 1, variables can be shadowed by inner scope declarations, as with `var x = 21`. Variable `x` in the outer scope remains accessible until the inner declaration shadows it, emphasizing scoping rules where the innermost declaration overtakes visible names unless modified before re-declaration .
JavaScript hoisting is a behavior where variable and function declarations are moved to the top of their containing scope during the compile phase. This affects outputs like in "Code 1" from Source 1, where variables declared with `var` are hoisted but their assignments are not, making `x` available in the scope after hoisting but before assignment, leading to undefined values in async functions .
In JavaScript, `Object.keys()`, `Object.values()`, and `Object.entries()` can convert objects to arrays, as noted in Source 1. `Object.keys()` gives an array of keys, useful for iterating over property names. `Object.values()` gives an array of values, suitable when values are needed without keys. `Object.entries()` provides key-value pairs and is optimal for paired data manipulation .
In JavaScript, `==` performs type coercion, determining false equality between `0` and `false`, as seen in Source 1. In contrast, `===` respects both value and type for strict equality, leading to more predictable results without automatic conversion, thus making `0 === false` false due to differing types .
In "Code 2" from Source 1, creating the same array upon each function call is inefficient. By moving the array creation outside the function into a closure or global scope, it ensures the array is created only once and reused, reducing memory usage and enhancing performance .
In JavaScript, object keys are always converted to strings. Hence, when setting `x[y]` and `x[z]` in "Code 1" from Source 1, both `y` and `z` are converted to the string 'object Object', leading the last assignment to overwrite previous ones. Thus `x[y]` results in `{name: 'Akki'}` regardless of the original object key .
Closures can capture the current environment's variables, effectively retaining their values within a function. Modifying "Code 3" in Source 1, enclosing the `setTimeout` in another function allows each iteration to retain its index value, achieving the desired output of 0 and 1 after one second by capturing `i` at its current state .