What are the builtin strings in JavaScript ?
Last Updated :
06 Feb, 2023
A sequence of letters, special characters, numbers, etc., or a combination of all of them is known as a string. Strings are created by enclosing the string characters within a single quote (') or within double quotes (").
Syntax:
var myString = 'Good Morning123!'; // Single quoted string
var myString = "Good Morning123!"; // Double quoted string
In Javascript, many string methods are either built-in or user-defined. Built-in string methods are the methods that are present in any programming language library.
Built-in string methods of JavaScript:
- search(): It is used to search a string for a specific value or an expression. It returns the position of the match.
- split(): It is used to split a string into an array of substrings.
- startsWith(): It is used to check whether a string begins with the specified characters.
- slice(): It is used to extract a part of a string and return a new string.
- concat(): It is used to combine texts of two strings and return a new string.
- charAt(): It is used to return the character at the specified index.
- indexOf(): It is used to return the index, within the string object, which occurs first, of the specified value. It returns -1 if the object is not found.
- lastIndexOf(): It is used to return the index, within the string object, which occurs last, of the specified value. It returns -1 if the object is not found.
- match(): It is used to match a regular expression against a string.
- replace(): It is used to find a match between a regular expression and a string. The matched substring is replaced with a new substring.
- substr(): It is used to return the characters in a string, beginning at the specified location, through the specified number of characters.
- substring(): It is used to return the characters in a string between the two specified indexes.
- toLowerCase(): It is used to convert the string value which is called, to lowercase.
- toUpperCase(): It is used to convert the string value which is called, to upper case.
- valueOf(): It is used to return the primitive value of the specified object.
- User-defined string methods in JavaScript:
- logIt(): It is used to log a parameter back to the console when the code is executed.
- return(): It is used to return a specific value, explicitly.
Below are some examples demonstrating the use of the Javascript string methods.
Example: This example shows the use of the Javascript string search() method.
HTML
<p id="demo"></p>
<script type="text/javascript" charset="utf-8">
function myFunction() {
var str = "Welcome to GeeksforGeeks!";
var a= str.search("GeeksforGeeks");
document.getElementById("demo").innerHTML = a;
}
myFunction()
</script>
Output:
11
Example: This example shows the use of the Javascript string split() method.
javascript
<p id="demo"></p>
<script type="text/javascript" charset="utf-8">
function myFunction() {
var str = "How are you feeling today?";
var res = str.split(" ");
document.getElementById("demo").innerHTML = res;
}
myFunction()
</script>
Output:
How, are, you, feeling, today?
Example: This example shows the use of the Javascript string startsWith() method.
javascript
<p id="demo"></p>
<script type="text/javascript" charset="utf-8">
function myFunction() {
var str = "Hello world, welcome to the universe.";
var n = str.startsWith("Hello");
document.getElementById("demo").innerHTML = n;
}
myFunction()
</script>
Output:
true
Similar Reads
What is trim in JavaScript ? JavaScript trim is a built-in function that is used to trim a string. A string is nothing but a group of characters. The trim ( ) removes whitespaces from both ends i.e. start and end of the string. We have to create an instance for a string class to invoke trim( ). It does not affect the original s
1 min read
Convert Array to String in JavaScript In JavaScript, converting an array to a string involves combining its elements into a single text output, often separated by a specified delimiter. This is useful for displaying array contents in a readable format or when storing data as a single string. The process can be customized to use differen
7 min read
String blink() Method in Javascript In Javascript, the blink() method is a string method that is used to display a string inside the blink tag. Syntax: string.blink() Note: This method has been DEPRECATED and is no longer recommended. Parameter Values: This method does not accept any parameters. Return Values: It returns a string valu
1 min read
How are strings stored in JavaScript ? In this article, we will try to understand how strings are stored in JavaScript. Strings are a primitive data type in JavaScript and are allocated a special place in memory to store and manipulate. Unlike some other languages, JavaScript does not have a String Constant Pool. Instead, JavaScript engi
3 min read
How to Create a String with Variables in JavaScript? To create a string with variables in JavaScript, you can use several methods, including string concatenation, template literals, and the String methods. Here are the common approaches:1. Template Literals (Recommended)Template literals are the modern and most preferred way to create strings with var
2 min read