String.startsWith() and String.endsWith() Methods
- The startsWith() method in JavaScript checks if a string starts with a specified substring, returning true if matched, otherwise false.
- The endsWith() method in JavaScript checks if a string ends with a given substring, returning true if matched, otherwise false.
Syntax:
str.startsWith( searchString , position ) // startsWith() method
str.endsWith(searchString, length) // endsWith() method
Example: Here is the basic example of above-mentioned method.
Javascript
let str1 = "Hello, Geeks!" ; console.log(str1.startsWith( "Hello" )); console.log(str1.startsWith( "Geeks" )); console.log(str1.endsWith( "Geeks!" )); console.log(str1.endsWith( "Hello" )); |
Output
true false true false
JS 2015 or ECMAScript 6 (ES6)
JS 2015 (ES6) also known as ECMAScript 6 (ES6), ECMAScript 6 (ES6) is a significant update to JavaScript, introducing arrow functions, classes, template literals, let and const for variable declaration, enhanced object literals, destructuring, and more modern features for better code organization and readability.