0% found this document useful (0 votes)
14 views6 pages

JavaScript String and Array Methods Guide

The document provides an overview of string and array methods in JavaScript, detailing their functionalities and examples. It covers various string methods such as charAt, includes, and replace, as well as array methods including push, map, and reduce. Each method is described with its purpose and a corresponding example for clarity.

Uploaded by

muhammadsaad5608
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

JavaScript String and Array Methods Guide

The document provides an overview of string and array methods in JavaScript, detailing their functionalities and examples. It covers various string methods such as charAt, includes, and replace, as well as array methods including push, map, and reduce. Each method is described with its purpose and a corresponding example for clarity.

Uploaded by

muhammadsaad5608
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

🧩 1.

STRING METHODS

🔹 Basic Info

Strings in JavaScript are immutable sequences of characters. Most methods return a new string
rather than modifying the original.

Method Description Example

charAt(index) Returns character at a "Hello".charAt(1) → "e"


specific index.

charCodeAt(index) Returns Unicode of "A".charCodeAt(0) → 65


character at given index.

at(index) Returns character at "Hi".at(-1) → "i"


index (supports negative
indexing).

concat(str1, str2, …) Joins multiple strings. "Hello".concat(" ", "World") → "Hello


World"

includes(substring) Checks if string contains "Hello".includes("el") → true


substring.

endsWith(suffix) Checks if string ends "[Link]".endsWith(".js") → true


with a certain substring.

startsWith(prefix) Checks if string starts "Hello".startsWith("He") → true


with a certain substring.

indexOf(substring) Returns first index of "banana".indexOf("a") → 1


substring.

lastIndexOf(substring) Returns last index of "banana".lastIndexOf("a") → 5


substring.

match(regex) Returns array of "abc123".match(/\d+/) → ["123"]


matches for a regex.

matchAll(regex) Returns iterator for all [... "abc123xyz456".matchAll(/\d+/g)]


regex matches. → [["123"], ["456"]]

padStart(length, str) Pads start of string with "5".padStart(3, "0") → "005"


given characters.
padEnd(length, str) Pads end of string with "5".padEnd(3, "0") → "500"
given characters.

repeat(count) Repeats the string given "ha".repeat(3) → "hahaha"


number of times.

replace(old, new) Replaces first match of "apple".replace("p", "b") → "bpple"


substring/regex.

replaceAll(old, new) Replaces all matches. "apple".replaceAll("p", "b") → "abble"

search(regex) Returns index of match "test123".search(/\d/) → 4


for regex.

slice(start, end) Extracts part of string. "Hello".slice(1, 4) → "ell"

substring(start, end) Similar to slice, but "Hello".substring(1, 4) → "ell"


negative indexes treated
as 0.

substr(start, length) (Deprecated) Extracts "Hello".substr(1, 3) → "ell"


substring of specific
length.

split(separator) Splits string into array. "a,b,c".split(",") → ["a","b","c"]

toLowerCase() Converts to lowercase. "HELLO".toLowerCase() → "hello"

toUpperCase() Converts to uppercase. "hello".toUpperCase() → "HELLO"

trim() Removes spaces from " Hi ".trim() → "Hi"


both ends.

trimStart() / trimLeft() Removes spaces from " Hi".trimStart() → "Hi"


start.

trimEnd() / trimRight() Removes spaces from "Hi ".trimEnd() → "Hi"


end.

valueOf() Returns primitive string (new String("abc")).valueOf() → "abc"


value.

toString() Returns string (123).toString() → "123"


representation.

localeCompare(other) Compares two strings "a".localeCompare("b") → -1


(locale aware).

normalize(form) Returns Unicode "\u00E9".normalize("NFD") → "é"


normalized form.

codePointAt(index) Returns Unicode code "😀".codePointAt(0) → 128512


point at position.

fromCharCode(num1, ... Returns string from UTF- [Link](65,66) → "AB"


) (static) 16 codes.

fromCodePoint(num1, .. Returns string from [Link](128512) → "😀"


.) (static) Unicode code points.

raw() (static) Used in template literals [Link]`Line\nbreak` → "Line\\


to get raw strings. nbreak"

🧮 2. ARRAY METHODS

🔹 Basic Info

Arrays are mutable, ordered lists. Some methods modify the array; others return a new array.

🔸 Creation and Access

Method Description Example

[Link](obj) Checks if object is an array. [Link]([1,2]) → true

[Link](iterable) Converts iterable/object to array. [Link]("abc") →


["a","b","c"]

[Link](...elements Creates array from arguments. [Link](1,2,3) → [1,2,3]


)

at(index) Access element by positive or [1,2,3].at(-1) → 3


negative index.
🔸 Adding / Removing Elements

Method Description Example

push(...items) Adds to end, returns new [Link](4)


length.

pop() Removes last element. [Link]()

unshift(...items) Adds to beginning. [Link](0)

shift() Removes first element. [Link]()

splice(start, Add/remove elements at [Link](2,1,"X")


deleteCount, ...items) index.

fill(value, start, end) Fills array with value. [1,2,3].fill(0) → [0,0,0]

copyWithin(target, start, Copies sequence to another [1,2,3,4].copyWithin(1,2) →


end) position. [1,3,4,4]

🔸 Iteration & Searching

Method Description Example

forEach(fn) Executes function for each element. [Link](x => [Link](x))

map(fn) Returns new array with transformed [1,2,3].map(x=>x*2) → [2,4,6]


values.

filter(fn) Returns new array with values passing [1,2,3].filter(x=>x>1) → [2,3]


condition.

find(fn) Returns first matching element. [1,2,3].find(x=>x>1) → 2

findIndex(fn) Returns index of first match. [1,2,3].findIndex(x=>x>1) → 1

findLast(fn) Returns last matching element. [1,2,3].findLast(x=>x>1) → 3

findLastIndex(fn) Returns index of last match. [1,2,3].findLastIndex(x=>x>1) →


2

some(fn) Returns true if any element passes [1,2,3].some(x=>x>2) → true


test.
every(fn) Returns true if all elements pass test. [1,2,3].every(x=>x>0) → true

includes(value) Checks if value exists. [1,2,3].includes(2) → true

indexOf(value) Returns first index of value. [1,2,3,2].indexOf(2) → 1

lastIndexOf(value Returns last index of value. [1,2,3,2].lastIndexOf(2) → 3


)

🔸 Sorting & Combining

Method Description Example

sort(fn) Sorts array (modifies original). [3,1,2].sort() → [1,2,3]

reverse() Reverses order (modifies [1,2,3].reverse() → [3,2,1]


array).

concat(...arrays) Merges arrays into a new one. [1].concat([2,3]) → [1,2,3]

join(separator) Joins elements into string. [1,2,3].join('-') → "1-2-3"

toString() Converts to string. [1,2].toString() → "1,2"

flat(depth) Flattens nested arrays. [1,[2,[3]]].flat(2) → [1,2,3]

flatMap(fn) Maps and flattens result. [1,2,3].flatMap(x=>[x,x*2]) →


[1,2,2,4,3,6]

🔸 Reduction & Accumulation

Method Description Example

reduce(fn, init) Reduces to single value (left → [1,2,3].reduce((a,b)=>a+b,0) → 6


right).

reduceRight(fn, Reduces right → left. [1,2,3].reduceRight((a,b)=>a-b) →


init) 0
🔸 Iteration Utilities

Method Description Example

keys() Returns iterator of indices. [...["a","b"].keys()] → [0,1]

values() Returns iterator of values. [...["a","b"].values()] → ["a","b"]

entries() Returns iterator of [index, value] [...[ "x", "y" ].entries()] → [[0,"x"],
pairs. [1,"y"]]

toReversed() Returns reversed copy (new in [1,2,3].toReversed() → [3,2,1]


ES2023).

toSorted() Returns sorted copy. [3,1,2].toSorted() → [1,2,3]

toSpliced() Returns spliced copy. [1,2,3].toSpliced(1,1,9) → [1,9,3]

with(index, Returns copy with element [1,2,3].with(1,9) → [1,9,3]


value) replaced.

You might also like