Query over curious return in Javascript

Bottom is a simple solution to an algorithm to which I will be referring. My question comes in a return I received when I altered the return to be result instead of result[0]
The console returned

['123, index: 0, input: ‘123aa1’, groups: undefined]
What is going on here? What kind of phenomenon have invoked by not including the index number [0] with result? I haven’t encountered this before.

function longestDigitsPrefix(inputString: string): string {
  const result = inputString.match(/^\d*/);
  if (result !== null) {
    return result[0];
    return "";

Although I’m not entirely sure I understand the question, you are essentially returning an Array when you return result (the type of the return value of match() is an array), but when you return result[0], you are returning the first element. You can read more about the match() function here:

Scroll down to the bottom and you will find a description of what “groups: undefined” means.

Feel free to explain your question further if I didn’t address it correctly.

1 Like