Lodash endsWith is a string utility function that checks if a string ends with a given target string. It provides enhanced functionality beyond the native JavaScript String.prototype.endsWith() method by supporting a position parameter that allows checking if the string ends with the target at a specific position rather than just at the end.
npm install lodash// Full lodash library
import _ from "lodash";
// Use as: _.endsWith(string, target, position)// Modular import (recommended)
import endsWith from "lodash/endsWith";
// Use as: endsWith(string, target, position)For CommonJS:
// Full library
const _ = require("lodash");
// Modular import
const endsWith = require("lodash/endsWith");import endsWith from "lodash/endsWith";
// Basic string ending check
endsWith("hello world", "world");
// => true
endsWith("hello world", "hello");
// => false
// Check with position parameter
endsWith("hello world", "hello", 5);
// => true (checks if "hello" ends at position 5)
// Edge cases
endsWith("test", "");
// => true (empty string always matches)
endsWith("", "test");
// => false
endsWith("abc", "c", 3);
// => true (position >= string length)Checks if a string ends with the given target string, with optional position parameter for enhanced control.
/**
* Checks if `string` ends with the given target string.
* @param {string} [string=''] - The string to search
* @param {string} [target] - The string to search for
* @param {number} [position=string.length] - The position to search from
* @returns {boolean} Returns `true` if `string` ends with `target`, else `false`
*/
function endsWith(string, target, position): boolean;Parameters:
string (string, optional): The string to search. null and undefined values are converted to empty string '', other values converted to string using coercion.target (string, optional): The string to search for. Gets converted to string using string coercion (target + '').position (number, optional): The position to search from. Defaults to string.length if undefined. Gets clamped to range [0, string.length].Returns:
boolean: Returns true if string ends with target at the specified position, else false.Behavior:
string and target parameterstrue regardless of positionindexOf method for string matchingUsage Examples:
import endsWith from "lodash/endsWith";
// Standard usage
endsWith("JavaScript", "Script");
// => true
endsWith("JavaScript", "Java");
// => false
// Position-based checking
endsWith("JavaScript", "Java", 4);
// => true (checks if "Java" ends at position 4)
endsWith("JavaScript", "Script", 6);
// => false ("Script" doesn't end at position 6)
// Edge cases
endsWith("test", "", 2);
// => true (empty target always matches)
endsWith("hello", "lo", 5);
// => true (position 5 equals string length)
endsWith("hello", "lo", 10);
// => true (position beyond length treated as string length)
// Type coercion examples
endsWith("123", 3);
// => true (number 3 converted to string "3")
endsWith(null, "");
// => true (null converted to empty string "")
endsWith(undefined, "");
// => true (undefined converted to empty string "")The endsWith function handles all parameter types gracefully through type coercion:
string values are converted to their string representationstarget values are converted using string coercionposition values are handled by clamping to valid range_.endsWith when lodash is loaded globally