JavaScript
Algorithms and Data Structures implemented in JavaScript for beginners, following best practices.
Top Related Projects
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
💯 Curated coding interview preparation materials for busy software engineers
Short code snippets for all your development needs
:bathtub: Clean Code concepts adapted for JavaScript
JavaScript Style Guide
📗🌐 🚢 Comprehensive and exhaustive JavaScript & Node.js testing best practices (July 2023)
Quick Overview
TheAlgorithms/JavaScript is a GitHub repository that provides implementations of various algorithms and data structures in JavaScript. It serves as an educational resource for developers to learn and understand different algorithmic concepts, offering a wide range of implementations from basic to advanced algorithms.
Pros
- Comprehensive collection of algorithms and data structures
- Well-organized codebase with clear folder structure
- Educational resource with commented code for better understanding
- Active community and regular updates
Cons
- Some implementations may not be optimized for production use
- Lack of extensive documentation for each algorithm
- Inconsistent coding style across different contributions
- Some algorithms may be missing or incomplete
Code Examples
- Binary Search implementation:
function binarySearch(arr, x, low = 0, high = arr.length - 1) {
if (low > high) return -1;
const mid = Math.floor((low + high) / 2);
if (arr[mid] === x) return mid;
if (arr[mid] > x) return binarySearch(arr, x, low, mid - 1);
return binarySearch(arr, x, mid + 1, high);
}
This code implements the binary search algorithm to find an element in a sorted array.
- Bubble Sort implementation:
function bubbleSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
This code implements the bubble sort algorithm to sort an array in ascending order.
- Fibonacci Sequence implementation:
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
This code implements a recursive function to generate the nth Fibonacci number.
Getting Started
To use the algorithms from this repository:
-
Clone the repository:
git clone https://github.com/TheAlgorithms/JavaScript.git
-
Navigate to the desired algorithm file.
-
Copy the function implementation into your project or import it directly:
import { binarySearch } from './path/to/BinarySearch.js'; const arr = [1, 3, 5, 7, 9]; const result = binarySearch(arr, 5); console.log(result); // Output: 2
Remember to adapt the import statement based on your project structure and the specific algorithm you want to use.
Competitor Comparisons
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
Pros of javascript-algorithms
- More comprehensive documentation with detailed explanations
- Better organized structure with categorized algorithms
- Includes Big O notation for time and space complexity
Cons of javascript-algorithms
- Less frequently updated compared to JavaScript
- Fewer contributors and community engagement
- More focused on educational purposes rather than production-ready code
Code Comparison
JavaScript (Binary Search):
function binarySearch(arr, x) {
let start = 0, end = arr.length - 1;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (arr[mid] === x) return mid;
else if (arr[mid] < x) start = mid + 1;
else end = mid - 1;
}
return -1;
}
javascript-algorithms (Binary Search):
function binarySearch(sortedArray, seekElement) {
let startIndex = 0;
let endIndex = sortedArray.length - 1;
while (startIndex <= endIndex) {
const middleIndex = startIndex + Math.floor((endIndex - startIndex) / 2);
if (sortedArray[middleIndex] === seekElement) {
return middleIndex;
}
if (sortedArray[middleIndex] < seekElement) {
startIndex = middleIndex + 1;
} else {
endIndex = middleIndex - 1;
}
}
return -1;
}
Both repositories offer valuable resources for learning and implementing algorithms in JavaScript. JavaScript provides a wider range of algorithms with more frequent updates, while javascript-algorithms offers more detailed explanations and better organization. The choice between the two depends on the user's specific needs and preferences.
💯 Curated coding interview preparation materials for busy software engineers
Pros of tech-interview-handbook
- Comprehensive guide covering various aspects of tech interviews, not just algorithms
- Includes soft skills, resume preparation, and interview strategies
- Regularly updated with community contributions and industry trends
Cons of tech-interview-handbook
- Less focus on in-depth algorithm implementations
- May not provide as many practical coding examples
- Broader scope may be overwhelming for those seeking specific algorithm practice
Code Comparison
tech-interview-handbook (Interview Preparation):
// Example of a coding question from the handbook
function twoSum(nums, target) {
const map = new Map();
for (let i = 0; i < nums.length; i++) {
const complement = target - nums[i];
if (map.has(complement)) {
return [map.get(complement), i];
}
map.set(nums[i], i);
}
}
JavaScript (Algorithm Implementation):
// Example of an algorithm implementation
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
The tech-interview-handbook provides a more holistic approach to interview preparation, while JavaScript focuses on algorithm implementations. Choose based on your specific needs and learning goals.
Short code snippets for all your development needs
Pros of 30-seconds-of-code
- Focuses on short, practical code snippets for quick implementation
- Covers a wide range of JavaScript utilities and helper functions
- Well-organized with clear categories and searchable interface
Cons of 30-seconds-of-code
- Less emphasis on in-depth algorithm explanations
- May not cover more complex or specialized algorithms
- Snippets are often optimized for brevity, which may sacrifice readability
Code Comparison
30-seconds-of-code snippet (Array chunking):
const chunk = (arr, size) =>
Array.from({ length: Math.ceil(arr.length / size) }, (_, i) =>
arr.slice(i * size, i * size + size)
);
JavaScript snippet (Array chunking):
function chunk(array, size) {
const chunked = [];
for (let i = 0; i < array.length; i += size) {
chunked.push(array.slice(i, i + size));
}
return chunked;
}
The 30-seconds-of-code version is more concise but may be less intuitive for beginners. The JavaScript version is more verbose but easier to understand and modify.
:bathtub: Clean Code concepts adapted for JavaScript
Pros of clean-code-javascript
- Focuses on best practices and coding standards for JavaScript
- Provides detailed explanations and examples for each principle
- Covers a wide range of topics, from variable naming to SOLID principles
Cons of clean-code-javascript
- Lacks implementation of actual algorithms
- May not be as practical for learning specific data structures or algorithms
- Less comprehensive in terms of code examples and implementations
Code Comparison
clean-code-javascript:
// Bad
const locations = ['Austin', 'New York', 'San Francisco'];
locations.forEach((l) => {
doStuff();
doSomeOtherStuff();
// ...
// ...
// ...
dispatch(l);
});
JavaScript:
function bubbleSort(arr) {
const len = arr.length;
for (let i = 0; i < len; i++) {
for (let j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
The code comparison shows that clean-code-javascript focuses on demonstrating good coding practices, while JavaScript provides actual algorithm implementations like bubble sort.
JavaScript Style Guide
Pros of airbnb/javascript
- Comprehensive style guide for JavaScript best practices
- Widely adopted in the industry, enhancing code consistency across projects
- Includes ESLint configuration for easy implementation
Cons of airbnb/javascript
- Focuses on coding style rather than algorithm implementations
- May be overwhelming for beginners due to its extensive rules
- Less hands-on learning compared to algorithm-focused repositories
Code Comparison
JavaScript:
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
airbnb/javascript:
// Good
const items = [1, 2, 3];
const itemsCopy = [...items];
// Bad
const len = items.length;
const itemsCopy = [];
for (let i = 0; i < len; i += 1) {
itemsCopy[i] = items[i];
}
The JavaScript repository focuses on algorithm implementations, while airbnb/javascript provides style guidelines and best practices for writing clean, maintainable JavaScript code. The code examples reflect this difference, with JavaScript showcasing a sorting algorithm and airbnb/javascript demonstrating preferred coding styles.
📗🌐 🚢 Comprehensive and exhaustive JavaScript & Node.js testing best practices (July 2023)
Pros of javascript-testing-best-practices
- Focuses specifically on testing best practices, providing comprehensive guidance
- Includes real-world examples and anti-patterns to avoid
- Regularly updated with community contributions and latest testing trends
Cons of javascript-testing-best-practices
- Limited to testing practices, not covering general JavaScript algorithms
- May be overwhelming for beginners due to its depth and breadth
- Lacks hands-on implementation of algorithms
Code Comparison
JavaScript:
function bubbleSort(arr) {
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
}
}
}
return arr;
}
javascript-testing-best-practices:
describe('Product service', () => {
describe('Add new product', () => {
it('Should return the added product id when a new product is saved', async () => {
const newProduct = {name: 'Test product', price: 100};
const result = await productService.add(newProduct);
expect(result).to.have.property('id');
});
});
});
This comparison highlights the different focus areas of the two repositories. JavaScript provides implementations of various algorithms, while javascript-testing-best-practices concentrates on testing methodologies and best practices for JavaScript projects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
The Algorithms - JavaScript
JavaScript Repository of TheAlgorithms, which implements various algorithms and data structures in JavaScript.
These implementations are for demonstrative purposes only. Dedicated implementations of these algorithms and data structures are much better for performance and security reasons. We also do not provide any guarantee for api stability.
Before contributing to this repository, make sure to read our Contribution Guidelines. You can look at other TheAlgorithms Repositories or the issues with a "help wanted" label for inspiration regarding what to implement. Our maintainers will guide you through how to make your contribution properly if you make any mistakes. The names of the maintainers of this repository are listed in the CODEOWNERS file.
You can find a list of the algorithms currently in the repository in the directory. Explanations of many of the algorithms can be found in the wiki.
Thanks to all the contributors â¤ï¸
Top Related Projects
📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings
💯 Curated coding interview preparation materials for busy software engineers
Short code snippets for all your development needs
:bathtub: Clean Code concepts adapted for JavaScript
JavaScript Style Guide
📗🌐 🚢 Comprehensive and exhaustive JavaScript & Node.js testing best practices (July 2023)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot