Strings
Accessing, searching, extracting, replacing, and transforming text values.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
.length |
String Length | Returns the number of characters in a string. | "JavaScript".length; // 10 |
string[index] |
Character Access | Returns the character at a specified zero-based index. | "JavaScript"[0]; // "J" |
.includes() |
Text Check | Checks whether a string contains specified text. | "JavaScript".includes("Script"); // true |
.indexOf() |
Text Position | Returns the index of the first occurrence of specified text, or -1 if it is not found. |
"JavaScript".indexOf("Script"); // 4 |
.startsWith() |
Beginning Check | Checks whether a string begins with specified text. | "JavaScript".startsWith("Java"); // true |
.endsWith() |
Ending Check | Checks whether a string ends with specified text. | "JavaScript".endsWith("Script"); // true |
.slice() |
Extract Text | Returns part of a string between specified indexes without changing the original string. | "JavaScript".slice(0, 4); // "Java" |
.substring() |
Extract Substring | Returns part of a string between specified indexes. | "JavaScript".substring(4, 10); // "Script" |
.replace() |
Replace Text | Returns a new string with text matching a string or regular expression replaced. | "Hello world".replace("world", "Jennifer"); |
.replaceAll() |
Replace All Matches | Returns a new string with all matching values replaced. | "one one".replaceAll("one", "two"); |
.trim() |
Remove Outer Whitespace | Returns a new string with whitespace removed from both ends. | " hello ".trim(); // "hello" |
.repeat() |
Repeat String | Returns a new string containing the original string repeated a specified number of times. | "Hi ".repeat(3); // "Hi Hi Hi " |
.toUpperCase() |
Uppercase Text | Returns a new string with alphabetic characters converted to uppercase. | "hello".toUpperCase(); // "HELLO" |
.toLowerCase() |
Lowercase Text | Returns a new string with alphabetic characters converted to lowercase. | "HELLO".toLowerCase(); // "hello" |
.split() |
Split String | Splits a string around a specified separator and returns the parts in an array. | "HTML,CSS,JavaScript".split(","); |
.charAt() |
Character at Index | Returns the character at a specified zero-based index. | "JavaScript".charAt(0); // "J" |
Numbers & Math
Formatting numbers and performing common rounding, comparison, randomization, and mathematical operations.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
.toFixed() |
Fixed Decimal Places | Formats a number using a specified number of decimal places and returns the result as a string. | (12.5).toFixed(2); // "12.50" |
Number.isInteger() |
Integer Check | Checks whether a value is a number with no fractional part. | Number.isInteger(42); // true |
Math.round() |
Round Number | Rounds a number to the nearest integer. | Math.round(4.6); // 5 |
Math.floor() |
Round Down | Rounds a number down to the nearest integer. | Math.floor(4.9); // 4 |
Math.ceil() |
Round Up | Rounds a number up to the nearest integer. | Math.ceil(4.1); // 5 |
Math.trunc() |
Remove Decimal Portion | Returns the integer portion of a number by removing its fractional digits. | Math.trunc(4.9); // 4 |
Math.abs() |
Absolute Value | Returns the absolute value of a number. | Math.abs(-10); // 10 |
Math.min() |
Minimum Value | Returns the smallest number from the provided values. | Math.min(4, 8, 2); // 2 |
Math.max() |
Maximum Value | Returns the largest number from the provided values. | Math.max(4, 8, 2); // 8 |
Math.random() |
Random Number | Returns a pseudorandom number greater than or equal to 0 and less than 1. |
Math.random(); |
Math.floor(Math.random() * max) |
Random Integer | Creates a random integer from 0 up to, but not including, a specified maximum. |
Math.floor(Math.random() * 10); // 0–9 |
Math.pow() |
Exponent | Raises a number to a specified power. | Math.pow(2, 3); // 8 |
Math.sqrt() |
Square Root | Returns the square root of a number. | Math.sqrt(64); // 8 |
Dates & Time
Creating, reading, updating, formatting, and comparing dates and times.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
new Date() |
Date Object | Creates a Date object representing a specific date and time or the current moment. | const now = new Date(); |
Date.now() |
Current Timestamp | Returns the current timestamp as milliseconds since January 1, 1970 UTC. | const timestamp = Date.now(); |
.getFullYear() |
Year | Returns the four-digit year of a Date. | date.getFullYear(); |
.getMonth() |
Month | Returns the zero-based month of a Date, where January is 0 and December is 11. |
date.getMonth(); |
.getDate() |
Day of Month | Returns the day of the month as a number from 1 through 31. |
date.getDate(); |
.getDay() |
Day of Week | Returns the zero-based day of the week, where Sunday is 0 and Saturday is 6. |
date.getDay(); |
.getHours() |
Hour | Returns the hour of a Date as a number from 0 through 23. |
date.getHours(); |
.getMinutes() |
Minutes | Returns the minutes of a Date as a number from 0 through 59. |
date.getMinutes(); |
.getSeconds() |
Seconds | Returns the seconds of a Date as a number from 0 through 59. |
date.getSeconds(); |
.getTime() |
Date Timestamp | Returns a Date's timestamp as milliseconds since January 1, 1970 UTC. | date.getTime(); |
< > |
Compare Dates | Compares Date objects chronologically to determine whether one occurs before or after another. | startDate < endDate; // true |
.setFullYear() |
Set Year | Changes the year of a Date and mutates the original Date object. | date.setFullYear(2027); |
.setMonth() |
Set Month | Changes the zero-based month of a Date and mutates the original Date object. | date.setMonth(5); |
.setDate() |
Set Day of Month | Changes the day of the month and mutates the original Date object. | date.setDate(15); |
.toISOString() |
ISO Date String | Returns a Date as a standardized ISO 8601 string in UTC. | date.toISOString(); |
.toLocaleDateString() |
Localized Date | Returns a locale-sensitive string representing the date portion of a Date. | date.toLocaleDateString(); |
.toLocaleTimeString() |
Localized Time | Returns a locale-sensitive string representing the time portion of a Date. | date.toLocaleTimeString(); |
Regular Expressions
Create patterns for searching, matching and replacing text in strings.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
/pattern/flags |
Regular Expression Literal | Creates a regular expression using literal syntax. | const pattern = /javascript/i; |
new RegExp() |
Regular Expression Constructor | Creates a regular expression from a string pattern and optional flags. | const pattern = new RegExp("javascript", "i"); |
.test() |
Pattern Check | Checks whether a regular expression matches part of a string. | /javascript/i.test("JavaScript"); // true |
.match() |
Find Matches | Returns matches between a string and a regular expression. | "JavaScript".match(/script/i); |
.replace() |
Replace Pattern Match | Returns a new string with text matching a regular expression replaced. | "hello world".replace(/world/i, "Jennifer"); |
.search() |
Find Match Position | Returns the index of the first regular expression match, or -1 if no match is found. |
"JavaScript".search(/script/i); // 4 |
g i m |
Common Flags | Changes how a regular expression searches, including global, case-insensitive and multiline matching. | const pattern = /javascript/gi; |
\d \w \s |
Character Classes | Matches common character groups such as digits, word characters and whitespace. | /\d+/.test("Order 42"); // true |
^ $ |
String Anchors | Matches the beginning or end of a string. | /^Hello/.test("Hello Jennifer"); // true |
* + ? { } |
Quantifiers | Controls how many times part of a pattern may repeat. | /\d{5}/.test("43016"); // true |
JSON
Converting between JSON text and JavaScript values.
| Term / Syntax | Name | Description | Example |
|---|---|---|---|
JSON.parse() |
Parse JSON | Parses JSON text and returns the corresponding JavaScript value. | const user = JSON.parse(json); |
JSON.stringify() |
Create JSON | Converts a JavaScript value into a JSON string. | const json = JSON.stringify(user); |