JavaScript recipes
84 tasks with working code, grouped by what you are working with.
Filter, sort, group, dedupe, chunk and transform arrays.
- Append one array to anotherHow to Extend Arrays in JavaScript
- Break out of forEachHow to Break from a JavaScript ForEach Loop
- Calculate the average of an arrayHow to Calculate Average of an Array in JavaScript
- Check if an array is emptyHow to Check if an Array is Empty in JavaScript
- Clone an arrayHow to Clone Arrays in JavaScript
- Count occurrences in an arrayHow to Count Elements with JavaScript
- Create an array of a given lengthHow to Create Arrays with a Specific Length in JavaScript
- Get the last element (index -1)How to Access -1 Index in JavaScript Arrays
Read, merge, copy and iterate over object properties.
- Access object propertiesHow to Access Object Properties in JavaScript
- Add a property to an objectHow to Add Properties to Objects in JavaScript
- Check if an object has a propertyHow to Check if an Object Has a Property in JavaScript
- Count the keys in an objectHow to Determine the Length of a Dictionary in JavaScript
- Flatten a nested objectHow to Flatten Objects in JavaScript
- Loop over key-value pairsHow to Iterate over Key-Value Pairs in JavaScript
- Merge two objectsHow to Combine Two Objects in JavaScript
- Use a variable as an object keyHow to Use a Variable as a Key in JavaScript
Search, replace, trim, pad and convert strings.
- Case-insensitive includesHow to Perform Case-Insensitive Includes in JavaScript
- Check if a string contains a substringHow to Check if a String Contains a Substring in JavaScript
- Check if a string is emptyHow to Check for an Empty String in JavaScript
- Check if a string starts withHow to Check if a String Begins with in JavaScript
- Convert a string to camelCaseHow to Use CamelCase in JavaScript
- Escape quotes in a stringHow to Escape Quotes in JavaScript
- Extract numbers from a stringHow to Extract Numbers from Strings in JavaScript
- Find all occurrences of a substringHow to Find All Occurrences with JavaScript
Round, format, parse and validate numbers.
- Check if a value is a numberHow to Determine if a Value is a Number in JavaScript
- Convert a float to an integerHow to Convert Floats to Integers in JavaScript
- Format a number with commasHow to Format Numbers with Commas in JavaScript
- Pad a number with leading zerosHow to Zero Pad Numbers in JavaScript
- Random number between two valuesHow to Generate Random Numbers Between Two Numbers in JavaScript
- Round to 2 decimal placesHow to Round to 2 Decimal Places in JavaScript
Add, subtract, compare and format dates and timestamps.
- Add days to a dateHow to Perform Date Addition in JavaScript
- Add months to a dateHow to Add Months to Dates in JavaScript
- Convert a timestamp to a dateHow to Convert Timestamps to Dates in JavaScript
- Difference between two datesHow to Subtract Dates in JavaScript
- Format a dateHow to Format JavaScript Datetime
- Format a date as YYYY-MM-DDHow to Format Dates as yyyy-mm-dd in JavaScript
- Get the current yearHow to Get Current Year in JavaScript
- Sort an array of datesHow to Sort Dates in JavaScript
Select, create, move and update elements on the page.
- Change an image sourceHow to Change Image Source in JavaScript
- Get an element's textHow to Access JavaScript Text Content
- Move an element to another parentHow to Move Elements with JavaScript
- Print a single elementHow to Print DIVs with JavaScript
- Replace an element's contentHow to Replace DIV Content with JavaScript
- Set element width and heightHow to Set Sizes with JavaScript
- Set the background colourHow to Set Background Color with JavaScript
- Uncheck a checkboxHow to Uncheck Checkboxes with JavaScript
Listen for, dispatch and simulate browser events.
Work with Map and Set collections.
Parse URLs, query strings and domains.
Read, parse and download files.
Viewport, clipboard, storage and other browser APIs.
Operators, conditionals, scope and language basics.
- Check if a function existsHow to Check if a Function Exists in JavaScript
- Check if a variable is definedHow to Check if a Variable is Defined in JavaScript
- Exit a function earlyHow to Exit Functions in JavaScript
- Less than or equal toHow to Compare Values with Less Than or Equal to in JavaScript
- Shorthand if/else (ternary)How to Use Shorthand If-Else Statements in JavaScript
- Swap two variablesHow to Swap Two Variables in JavaScript
- What curly braces doHow to Use Curly Braces in JavaScript
import, export, ESM and CommonJS.
Print and inspect values while debugging.
Classic algorithms implemented in JavaScript.
Server-side scripts, processes and the file system.