Advertisement
PortalPlayer

how javascripts startsWith function works

Jul 27th, 2020 (edited)
1,542
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const startsWith = (stringValue, searchValue, position) => {
  2.     if (typeof stringValue === "string" && (["string", "boolean", "number"].includes(typeof searchValue) || searchValue instanceof Array)) {
  3.         if (position) return stringValue.slice(position).slice(0, searchValue.toString().length) === searchValue.toString()
  4.         else return stringValue.slice(0, searchValue.toString().length) === searchValue.toString()
  5.     } else return false
  6. }
  7.  
  8. // example
  9. // code | expected output
  10.  
  11. startsWith("testing oof", "testing") // true
  12. startsWith("testing oof", "oof", 8)  // true
  13.                                      //
  14. startsWith()                         // false
  15. startsWith("testing oof")            // false
  16. startsWith("testing oof", "oof")     // false
  17. startsWith("testing oof", "oof", 10) // false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement