The startsWith function is a feature of strings that checks whether a string commences with particular characters. It then returns either true or false based on the outcome.
In this section, we will explore the functionality of the startsWith method by providing examples to demonstrate its usage.
JavaString String startsWith Method
The function will return a boolean value of true if it detects that the initial characters of the provided string match the specified characters in another string; otherwise, it will return false. It is important to note that the startsWith function is sensitive to letter case.
Syntax
str.startsWith(searchString[, position])
In the above syntax:
- str: The specified string
- searchString: The characters are specified which are to be searched in the str.
- position: An optional parameter that holds the position in the string from where to begin the searching. By default, the value is 0.
Return Value
The function string evaluates to true when the indicated characters are present within the string; otherwise, it resolves to false.
Example of String startsWith Method
Let's explore a few sample implementations to enhance our comprehension of the concept:
Example 1:
Below is an example code:
<html>
<body>
<script>
const test = 'Work is Worship';
console.log(test.startsWith('Work'));
console.log(test.startsWith('Worship',8));
console.log(test.startsWith('work'));
</script>
</body>
</html>
Within the provided code snippet, a string has been allocated to a constant variable named 'test':
- log(test.startsWith('Work'));
When we use console.log to display the string with searchString = Work and do not specify a position, the searchString will start searching for its value from the 0th position. As it finds the characters at the beginning of the search, it will return true as the output.
- log(test.startsWith('Worship', 8));
When we use console log to display the string with the searchString set to "Worship" and the position value specified as 8, the search will start from the 8th character onwards. In this case, when the search reaches the 8th position, it detects the matching characters, resulting in a true output being returned.
- log(test.startsWith('work'));
When we use console.log to display the string with searchString set to "work" without a specified position, the search starts from the 0th position. Since the first character does not match, it returns false. The reason for this false output is that the startsWith method in JavaScript is sensitive to the case of the characters.
Output:
Note: The default position value 0 starts from the first character of the string. Thus, if the 0 th position in the string is a single space, the output will be 0 with the above searchString values.
Example 2:
Below is a code example:
<html>
<body>
<script>
function test() {
var str = 'Work is Worship';
var x = str.startsWith('Wor');
console.log(x);
}
test();
</script>
</body>
</html>
Output:
In the above code,
- We have created a function test within which we have initialized a string for variable str .
- Next, we have specified some character string that is to be searched in the specified string.
- We have not set the position value, and so it searches from the 0 th position in the string.
- As it finds that the specified characters match with the string characters specified in the string. So, it returns the output as true because the match is found. Thus, the value of the variable x is assigned as true . Hence, on invoking the function, the output is returned as true.