
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Return TRUE if the first string starts with a specific second string JavaScript
We are required to write a JavaScript function that takes in two strings and checks whether first string starts with second or not
For example −
If the two strings are: “Disaster management report” “Disas” Then our function should return true
Let's write the code for this function −
Example
const first = 'The game is on'; const second = 'The'; const startsWith = (first, second) => { const { length } = second; const { length: l } = first; const sub = first.substr(0, length); return sub === second; }; console.log(startsWith(first, second));
Output
The output in the console will be −
true
- Related Articles
- Is the second string a rotated version of the first string JavaScript
- Return a boolean array which is True where the string element in array starts with prefix in Python
- Find if a string starts and ends with another given string in C++
- Remove all characters of first string from second JavaScript
- Dynamic Programming: Is second string subsequence of first JavaScript
- How to check if a string starts with a specified Prefix string in Golang?
- Delete elements in first string which are not in second string in JavaScript
- Check if a string starts with given word in PHP
- Return index of first repeating character in a string - JavaScript
- How to check if string or a substring of string starts with substring in Python?
- Check if a String starts with any of the given prefixes in Java
- Return a specific MySQL string using regular expressions
- Generating random string with a specific length in JavaScript
- Return true or false in a MySQL select if another field contains a string?
- Python Program to check if a string starts with a substring using regex

Advertisements