DEV Community

JavaScript Interview Questions

Prajesh Gawhale on January 17, 2025

Hi folks! ๐Ÿ‘‹ Welcome to yet another session of free ka gyan ๐Ÿ’ก (because who doesnโ€™t love free wisdom?). You might be thinking, โ€œWhy another session?โ€...
Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข

Your string reversing code is using split. This will break with some unicode characters (try reversing a string containing emojis). Much better to spread the string to an array ([...str]) - not perfect, but much better.

Collapse
 
prajesh_kun profile image
Prajesh Gawhale โ€ข

Hi Jon,

Thank you for your input! You are absolutely correct that using splitwill not handle Unicode characters like emojis or certain symbols effectively. Instead, ([...str]) is indeed a better option for such cases.

To elaborate for the readers, when using split, the string is divided into individual characters based on UTF-16 code units. Emojis, however, are surrogate pairs consisting of two UTF-16 code units, which causes splitto fail. On the other hand, the spread operator treats Unicode characters as single entities, regardless of how many UTF-16 code units they consist of.

I appreciate you pointing this out, and Iโ€™ll also edit the code to ensure it handles Unicode characters properly.

Thank you again for sharing your thoughts!

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข

What are Self-Invoking Functions, or Immediately Invoked Function Expressions (IIFE), also known as Anonymous Functions?

IIFEs are not self invoking (that would make them recursive functions), and they don't have to be anonymous

Collapse
 
prajesh_kun profile image
Prajesh Gawhale โ€ข โ€ข Edited

Hi Jon,

You're almost right! Just a small clarification: IIFEs are "Immediately Invoked Function Expressions" because they execute immediately after being defined, but this doesn't make them recursive. Recursion occurs when a function calls itself from within its own body. Also, you're absolutely correct that IIFEs don't have to be anonymousโ€”they can be named as well.

Small examples:
Anonymous IIFE:

(function() {
  console.log("This is an anonymous IIFE!");
})();
Enter fullscreen mode Exit fullscreen mode

Named IIFE:

(function namedIIFE() {
  console.log("This is a named IIFE!");
})();
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข โ€ข Edited

Self invoking would mean they invoke themselves. They do not. They are invoked the same way as any other function - from outside (i.e. not by the function itself)

A "self invoking function" is - by definition - recursive.

Calling IIFEs self-invoking is entirely wrong.

Thread Thread
 
prajesh_kun profile image
Prajesh Gawhale โ€ข

You're correct in distinguishing between IIFEs (Immediately Invoked Function Expressions) and recursive functions, and the misuse of the term "self-invoking."

An IIFE is not "self-invoking" in the literal sense. It's explicitly invoked by the surrounding parentheses (()), which execute the function immediately upon definition. The function does not invoke itself; it is invoked by wrapping syntax.

On the other hand, recursive functions do indeed "self-invoke" because they call themselves within their own body, typically with modified arguments, until a base case is met to stop the recursion. I hope that clarifies the statement.

Collapse
 
bobbystacks profile image
Robert Strickland โ€ข

Just now learning Javascript hopefully I can learn most of these values soon to better understand these questions.

Collapse
 
prajesh_kun profile image
Prajesh Gawhale โ€ข

That's a wonderful approach! JavaScript can be a bit challenging initially, but with consistent practice, you'll master it in no time. One piece of advice: try to learn concepts through practical examples. Feel free to reach out if you have any questions or need help along the way. You've got this!

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข

What is pass-by-value and pass-by-reference?

Be careful answering this one. JavaScript only has pass by value. Kind of a trick question

Collapse
 
prajesh_kun profile image
Prajesh Gawhale โ€ข

Hi Jon!

Thank you for sharing your thoughts! I just wanted to clarify a small point about JavaScript. While it's a common misconception, JavaScript technically doesn't use pass-by-reference. Instead, it uses pass-by-value for everything, but with objects (including arrays and functions), the "value" that's passed is actually a reference to the memory location. This is why it sometimes behaves like pass-by-reference.

To break it down:
Pass-by-Value (Primitives): A copy of the value is passed, so changes inside the function donโ€™t affect the original variable (e.g., numbers, strings, booleans).

Objects (Reference-like behavior): A copy of the reference is passed, which allows changes to the object's properties to reflect outside the function. However, if you reassign the parameter inside the function, the original reference remains unchanged.

Itโ€™s a subtle distinction, but itโ€™s a good one to keep in mind! Hope this helps! ๐Ÿ˜Š

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ โ€ข

The polyfills for map and filter are both missing the optional thisArg. Without this, they're not viable polyfills for these functions.

Collapse
 
prajesh_kun profile image
Prajesh Gawhale โ€ข

Thank you for pointing this out! Youโ€™re absolutely correctโ€”the current polyfills for mapand filterdo not include support for the optional thisArgparameter, which is essential for fully replicating the behavior of these functions.

The thisArgallows developers to provide a custom this context for the callback function, which can be crucial in scenarios where the callback depends on a specific execution context. Without it, the polyfills may fail to handle certain use cases properly, making them incomplete.

I really appreciate you bringing this to my attention and highlighting its importance!