JavaScript String Referentiemanual
- Vorige pagina JS Set
- Volgende pagina JS Objecten
JavaScript string
JavaScript strings store a series of characters, such as "Bill Gates".
Strings can be any text within double or single quotes:
let carName1 = "Volvo XC60"; let carName2 = 'Volvo XC60';
String indices (subscripts) start from zero:
The first character is at position 0, the second at 1, and so on.
For knowledge about strings, please visit our JavaScript string tutorial.
String properties and methods
Normally, strings like 'Bill Gates' cannot have methods or properties because they are not objects.
But for JavaScript, methods and properties can also be used for strings, because JavaScript treats strings as objects when executing methods and properties.
JavaScript string methods and properties
Methods | Description |
---|---|
at() | Return the character at the specified index in the string. |
charAt() | Return the character at the specified index (position) in the string. |
charCodeAt() | Return the Unicode value of the character at the specified index (position) in the string. |
codePointAt() | Return the Unicode value at the specified index (position) in the string. |
concat() | Return two or more concatenated strings. |
constructor | Return the constructor of the string. |
endsWith() | Return whether the string ends with the specified value. |
fromCharCode() | Return the Unicode value as a character. |
includes() | Return whether the string contains the specified value. |
indexOf() | Return the index (position) of the first occurrence of the specified value in the string. |
lastIndexOf() | Return the index (position) of the last occurrence of the specified value in the string. |
length | Return the length of the string. |
localeCompare() | Compare two strings in the current locale. |
match() | Search for a value or regular expression in the string and return the match. |
padEnd() | Fill characters at the end of the string. |
padStart() | Fill characters from the beginning of the string. |
prototype | Allows you to add properties and methods to an object. |
repeat() | Return a new string containing a specified number of string copies. |
replace() | Searches for a pattern in the string and returns the string with the first match replaced. |
replaceAll() | Searches for a pattern in the string and returns a new string with all matches replaced. |
search() | Searches for a value or regular expression in the string and returns the index (position) of the match. |
slice() | Extracts a part of the string and returns a new string. |
split() | Splits the string into an array of substrings. |
startsWith() | Checks if the string starts with a specified character. |
substr() | Extracts a specified number of characters starting from the specified index (position) in the string. |
substring() | Extracts characters between two specified indices (positions) in the string. |
toLocaleLowerCase() | Converts the string to lowercase letters using the host's locale and returns it. |
toLocaleUpperCase() | Converts the string to uppercase letters using the host's locale and returns it. |
toLowerCase() | Returns the string converted to lowercase letters. |
toString() | Returns the string or string object as a string. |
toUpperCase() | Returns the string converted to uppercase letters. |
trim() | Returns the string with spaces removed. |
trimEnd() | Returns the string with trailing spaces removed. |
trimStart() | Returns the string with leading spaces removed. |
valueOf() | Returns the original value of the string or string object. |
Tip:All string methods return a new value. They do not change the original variable.
String HTML Wrapper Method
HTML wrapper methods return strings wrapped in HTML tags.
These are not standard methods and may not work as expected.
Methods | Description |
---|---|
anchor() | Display the string as an anchor. |
big() | Display the string in large font. |
blink() | Display the string blinking. |
bold() | Display the string in bold. |
fixed() | Display the string with a fixed spacing font. |
fontcolor() | Display the string with a specified color. |
fontsize() | Display the string with a specified size. |
italics() | Display the string in italics. |
link() | Display the string as a hyperlink. |
small() | Display the string in small font. |
strike() | Display the string with a line through it. |
sub() | Display the string as subscript text. |
sup() | Display the string as superscript text. |
String object description
Strings zijn een basis datatype in JavaScript. De String-klasse biedt methoden om met de oorspronkelijke stringwaarden te werken.
String-objecten van Lengte-eigenschapVerklaart het aantal karakters in de string.
De String-klasse definieert een groot aantal methoden voor het manipuleren van strings, zoals het ophalen van karakters of substringen uit een string, of het verkrijgen van informatie over karakters of substringen.
Opmerking:JavaScript strings zijn onveranderlijk (immutable), de methoden die door de String-klasse worden gedefinieerd, kunnen de inhoud van de string niet wijzigen. Zoals String.toUpperCase() Dergelijke methoden retourneren een nieuwe string, niet het wijzigen van de oorspronkelijke string.
In de eerdere Netscape-codebasis JavaScript-implementaties (bijvoorbeeld in Firefox-implementaties), gedraagt een string zich als een leesbare karakterarray. Bijvoorbeeld, het ophalen van de derde karakter van een string s kan worden vervangen door s[2] in plaats van de meer standaard s.charAt(2). Bovendien, wanneer een for/in-cyclus wordt toegepast op een string, worden de arrayindices van elk karakter in de string geenumereerd (maar let op, het ECMAScript-standaard bepaalt dat de length-eigenschap niet kan worden geenumereerd). Omdat het arraygedrag van strings niet standaard is, moet het worden vermeden.
Boeken voor buiten de les
Voor meer informatie, lees de relevante inhoud in de JavaScript Avanceerde Handleiding:
- ECMAScript Referentietypen
- Referentietypen worden meestal genoemd als klassen (class) of objecten. Dit hoofdstuk bespreekt de vooraf gedefinieerde referentietypen van ECMAScript.
- Vorige pagina JS Set
- Volgende pagina JS Objecten