This repository contains notes and code snippets related to JavaScript programming, covering fundamental concepts, core libraries, and best practices. It serves as a personal reference for learning and consolidating knowledge in JavaScript, intended for personal use to reinforce understanding of JavaScript concepts.
This repository explores various JavaScript topics, including but not limited to:
- Fundamentals: Core JavaScript concepts.
- Data Structures: Sets, Lists, and related concepts.
- Composition: Function composition and its applications.
- Error Handling: Techniques for handling errors in JavaScript, including
try-catch
. - Higher-Order Functions: Working with functions that operate on other functions.
- Scope: Understanding variable scope in JavaScript.
- Hoisting: How variable and function declarations are hoisted.
- Object-Oriented Programming (OOPs) in JavaScript: Exploring OOP concepts in the context of JavaScript.
- Arrays and Strings: Working with arrays and strings in JavaScript.
This repository is primarily for personal reference. Feel free to browse the files and code snippets. If you find anything useful, you are welcome to adapt it for your own learning. However, keep in mind that this repository is a work in progress and may be updated or modified over time.
Contributions are not expected as this is a personal learning repository.
This repository is intended for personal use and the content within is not subject to a formal license. However, please be respectful of any code or ideas that may be derived from other sources.
- Definition: Arrays are ordered collections of values. They can hold values of any data type (including other arrays).
- Creation:
let myArray = [];
(Empty array)let myArray = [1, 2, "three", { key: "value" }];
(Array with mixed data types)let myArray = new Array(1, 2, 3);
(Using theArray
constructor)
- Indexing: Elements are accessed using zero-based indexing (e.g.,
myArray[0]
for the first element). - Length: The
length
property returns the number of elements in the array.
push(element)
: Adds an element to the end of the array. Returns the new length.pop()
: Removes the last element from the array. Returns the removed element.unshift(element)
: Adds an element to the beginning of the array. Returns the new length.shift()
: Removes the first element from the array. Returns the removed element.splice(startIndex, deleteCount, ...items)
:- Removes elements from
startIndex
withdeleteCount
. - Inserts
items
atstartIndex
. - Returns an array of the removed elements.
- Removes elements from
fill(value, startIndex, endIndex)
: Fills a portion of the array with a static value.reverse()
: Reverses the order of the elements in the array. Modifies the original array.sort(compareFunction)
: Sorts the elements of the array. Modifies the original array.- If
compareFunction
is omitted, elements are sorted lexicographically (string comparison). - For numeric sorting:
array.sort((a, b) => a - b);
(ascending),array.sort((a,b) => b-a);
(descending)
- If
slice(startIndex, endIndex)
: Returns a shallow copy of a portion of the array. Does not modify the original array.concat(...arrays)
: Returns a new array that is the concatenation of the original array and the given arrays. Does not modify the original arrays.join(separator)
: Returns a string by concatenating all of the elements in an array, separated by the specified separator.indexOf(element, startIndex)
: Returns the first index at which a given element can be found in the array, or -1 if it is not present.lastIndexOf(element, startIndex)
: Returns the last index at which a given element can be found in the array, or -1 if it is not present.includes(element, startIndex)
: Determines whether an array includes a certain element, returningtrue
orfalse
.toString()
: Returns a string representing the array and its elements.
forEach(callback)
: Executes a provided function once for each array element.map(callback)
: Creates a new array with the results of calling a provided function on every element in the calling array.filter(callback)
: Creates a new array with all elements that pass the test implemented by the provided function.reduce(callback, initialValue)
: Executes a reducer function (provided by you) on each element of the array, resulting in a single output value.reduceRight(callback, initialValue)
: Applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.some(callback)
: Tests whether at least one element in the array passes the test implemented by the provided function. Returns a Boolean value.every(callback)
: Tests whether all elements in the array pass the test implemented by the provided function. Returns a Boolean value.find(callback)
: Returns the value of the first element in the provided array that satisfies the provided testing function.findIndex(callback)
: Returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1.entries()
: Returns a new Array Iterator object that contains the key/value pairs for each index in the array.
let [first, second, ...rest] = myArray;
(Extracts elements into variables)
let newArray = [...myArray, 4, 5];
(Creates a new array with elements frommyArray
and additional elements)
arguments
object in functions.- DOM NodeLists.
- Can be converted to arrays using
Array.from()
or the spread operator.
- Arrays containing other arrays.
- Access elements using multiple indices (e.g.,
my2DArray[row][column]
).
- Array methods are crucial. Practice using
map
,filter
,reduce
,forEach
, andsort
. - Understand the difference between methods that modify the original array and those that return a new array.
- Be comfortable with array destructuring and the spread operator.
- Know how to handle array-like objects.
- Understand the time complexity of common array operations.