Syntax, Parameter and Return Value
Syntax:
_.startsWith([string=''], [target], [position=0])
Parameters:
-
[string=''] (string): The string to inspect.
-
[target] (string): The string to search for.
- [position=0] (number): The position to start the search at.
Return Value:
(boolean) - Returns true if string starts with target, else false.
Examples
Checking for a Specific Prefix in a String:
JavaScript
var _ = require('lodash');
var text = 'hello world';
console.log(_.startsWith(text, 'hello'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Demonstrates checking if a string starts with a specific word.
Validating URL Protocols:
JavaScript
var url = 'https://www.example.com';
console.log(_.startsWith(url, 'https'));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
Shows how to validate the protocol of a URL.
Searching from a Specific Position:
JavaScript
var sentence = 'This is a test';
console.log(_.startsWith(sentence, 'is', 2));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
An example of using the position parameter to check for a substring starting at a specific index in the string.
Filtering Data Based on Prefix:
JavaScript
var filenames = ['report.txt', 'image.jpg', 'script.js'];
var txtFiles = filenames.filter(name => _.startsWith(name, 'report'));
console.log(txtFiles);

You can also try this code with Online Javascript Compiler
Run Code
Output:
['report.txt']
Demonstrates filtering an array of strings based on a prefix.
Frequently Asked Questions
How does _.startsWith() differ from using String.prototype.startsWith()?
_.startsWith() is Lodash's implementation and is similar to JavaScript's native startsWith() method. The primary difference is that Lodash methods like _.startsWith() can be chained with other Lodash utilities for more complex operations.
Can _.startsWith() handle case-sensitive checks?
_.startsWith() is case-sensitive. For case-insensitive checks, you should convert both the string and target to the same case (e.g., using .toLowerCase()).
Is _.startsWith() suitable for checking multiple possible starts?
For multiple starts, _.startsWith() would need to be used in conjunction with other methods, like Array.prototype.some(), to check each potential start string.
Conclusion
Lodash's _.startsWith() method is an effective tool for verifying whether a string begins with a specified substring. It's particularly useful for string validation, searching, and filtering tasks where the start of the string is significant.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.