Skip to Main Content

Find the Index of the First Occurrence in a String

Problem URL:Find the Index of the First Occurrence in a String

My Solution

JavaScript

/**
 * @param {string} haystack
 * @param {string} needle
 * @return {number}
 */

const strStr = (haystack, needle) => {
  if (needle.length === 0) {
    return 0;
  }

  return haystack.indexOf(needle);
};

Let's Connect

Twitter GitHub LinkedIn