Home >Web Front-end >JS Tutorial >How to use startwith function
The startwith function is used to check whether the given string starts with the characters of the specified string. Its usage syntax is "str.startsWith(searchString, position)", and the parameter searchString represents the string.
The operating environment of this article: Windows 7 system, Dell G3 computer, javascript version 1.8.5.
The str.startsWith() function in JavaScript is used to check whether the given string starts with the characters of the specified string. Let's take a look at the specific usage of the startsWith() function.
Let’s first take a look at the basic syntax of the startsWith() function
str.startsWith(searchString , position)
The first parameter searchString of the startsWith() function represents a string, which will be given Search at the end of the string. The second parameter of the function is position, which determines the position within the given string where searchString is searched.
This function returns a Boolean value true if the searchString is found, otherwise it returns a Boolean value false.
Let’s look at the specific example
The code is as follows
Check whether the string str starts with It
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> function func() { var str = 'It is a great day.'; var value = str.startsWith('It'); document.write(value); } func(); </script> </body> </html>
Output results For: true
Check whether the string str starts with great
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> function func() { var str = 'It is a great day.'; var value = str.startsWith('great'); document.write(value); } func(); </script> </body> </html>
The output result is: false
Check the string Whether str starts with great at the specified index 8
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <script> function func() { var str = 'It is a great day.'; var value = str.startsWith('great',8); document.write(value); } func(); </script> </body> </html>
The output result is: true
This article ends here. For more exciting content, you can pay attention to php Chinese Other related column tutorials on the Internet! ! !
The above is the detailed content of How to use startwith function. For more information, please follow other related articles on the PHP Chinese website!