JavaScript String Reference
In this article, we will learn JavaScript Strings and their properties and methods with a brief description. JavaScript Strings are used for storing and manipulating text content. It can contain zero or more characters within single or double quotes, like “w3wiki” or ‘w3wiki’.
Syntax:
String(object)
Example: In this example, we will return the length of a string.
Javascript
function stringLength() { const str = "w3wiki" ; console.log( "String Length: " + str.length); } stringLength(); |
Output:
String Length: 13
Example: In this example, we will return the Date() object’s string value.
Javascript
// Date Object having date and time const currentDate = new Date() // date object's string value const currentDate_String = currentDate.toString() console.log( "Date Object's String Value: " + currentDate_String) |
Date Object's String Value: Wed Sep 06 2023 21:12:54 GMT+0000 (Coordinated Universal Time)
The complete list of JavaScript Strings has listed below:
JavaScript String Constructor: In JavaScript, a constructor gets called when an object is created using the new keyword.
Constructor |
Description |
Example |
---|---|---|
String() | Convert the value of an object to a string. |
JavaScript String Properties: A JavaScript property is a member of an object that associates a key with a value. There is no static property in the string only instance property.
- Instance Property: An instance property is a property that has a new copy for every new instance of the class.
Instance Properties |
Description |
Example |
---|---|---|
length | Find out the size of that object | |
constructor | Return the string constructor function for the object. |
JavaScript String Methods: JavaScript methods are actions that can be performed on objects. There are two types of String methods in JavaScript.
- Static Method: If the method is called using the string class itself then it is called a static method.
Static Methods |
Description |
Example |
---|---|---|
fromCharCode() | Create a string from the given sequence of UTF-16 code units. | |
fromCodePoint() | Return a string or an element for the given sequence of code point values (ASCII value). |
- Instance Method: If the method is called on an instance of a string then it is called an instance method.
Instance Methods |
Description |
Example |
---|---|---|
at() | Find the character at the specified index. | |
anchor() | Creates an anchor element that is used as a hypertext target. | |
charAt() | Returns that character at the given index of the string. | |
charCodeAt() | Returns a Unicode character set code unit of the character present at the index in the string. | |
codePointAt() | Return a non-negative integer value i.e, the code point value of the specified element. | |
concat() | Join two or more strings together in JavaScript. | |
endsWith() | Whether the given string ends with the characters of the specified string or not. | |
includes() | Returns true if the string contains the characters, otherwise, it returns false. | |
indexOf() | Finds the index of the first occurrence of the argument string in the given string. | |
lastIndexOf() | Finds the index of the last occurrence of the argument string in the given string. | |
localeCompare() | Compare any two elements and returns a positive number | |
match() | Search a string for a match against any regular expression. | |
matchAll() | Return all the iterators matching the reference string against a regular expression. | |
normalize() | Return a Unicode normalization form of a given input string. | |
padEnd() | Pad a string with another string until it reaches the given length from rightend. | |
padStart() | Pad a string with another string until it reaches the given length from leftend. | |
repeat() | Build a new string containing a specified number of copies of the string. | |
replace() | Replace a part of the given string with some another string or a regular expression | |
replaceAll() | Returns a new string after replacing all the matches of a string with a specified string/regex. | |
search() | Search for a match in between regular expressions and a given string object. | |
slice() | Return a part or slice of the given input string. | |
split() | Separate given string into substrings using a specified separator provided in the argument. | |
startsWith() | Check whether the given string starts with the characters of the specified string or not. | |
substr() | Returns the specified number of characters from the specified index from the given string. | |
substring() | Return the part of the given string from the start index to the end index. | |
toLowerCase() | Converts the entire string to lowercase. | |
toLocaleLowerCase() | Returns the calling string value converted to a lowercase letter. | |
toLocaleUpperCase() | Returns the calling string value converted to a uppercase letter. | |
toUpperCase() | Converts the entire string to uppercase. | |
toString() | Return the given string itself. | |
trim() | Remove the white spaces from both ends of the given string. | |
trimEnd() | Remove white space from the end of a string. | |
trimStart() | Remove white space from the start of a string. | |
valueOf() | Return the value of the given string. |