Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
Javascript String startsWith()
In this tutorial, we will learn about the JavaScript String startsWith() method with the help of examples.
The startsWith() method returns true if a string begins with specified character(s). If not, it returns false.
Example
const message = "JavaScript is fun";
// check if message starts with Java
let result = message.startsWith("Java");
console.log(result); // true
// check if message starts with Script
result = message.startsWith("Script");
console.log(result); // false
startsWith() Syntax
The syntax of the startsWith() method is:
str.startsWith(searchString, position)
Here, str is a string.
startsWith() Parameters
The startsWith() method takes in :
- searchString - The characters to be searched for at the beginning of str.
- position (optional) - The position in str to start searching for searchString. Default value is 0.
startsWith() Return Value
- Returns
trueif the given characters are found at the beginning of the string. - Returns
falseif given characters are not found at the beginning of the string.
Note: The startsWith() method is case sensitive.
Example: Using startsWith() method
sentence = "Java is to JavaScript what Car is to Carpet.";
let check = sentence.startsWith("Java");
console.log(check); // true
let check1 = sentence.startsWith("Java is");
console.log(check1); // true
// case sensitive
let check2 = sentence.startsWith("JavaScript");
console.log(check2); // false
// second argument specifies the starting position
let check3 = sentence.startsWith("JavaScript", 11);
console.log(check3); // true
Output
true true false true
Also Read:
Did you find this article helpful?