JavaScript Functions vs LINQ C#

Introduction 

 
I have been working with C# for 10 years, and in the last 4 years, I started to use JS in the front-end into deep.
 
I want to compare some methods related to arrays and collection in JS and C# (using LINQ). I want to show you how to choose the right method depending on your requirements in JS or C#.
 

forEach (js) vs ForEach (C# not LINQ)

 
This method is used to execute a function for each element in the array or collection. You can use it to update each element depending on conditions or for getting specific values.

Both methods are not a pure function, which means the original collection is affected or updated. This method is not included in LINQ (it’s on C# collections directly) but it’s important to mention it.
  1. //JS demo  
  2. const array1 = [1, 2, 3, 4, 5];  
  3. array1.forEach(element => console.log(element));  
  4.   
  5. //C# demo  
  6. var listOfNumbers = new int[] {0,1,2,3,4,5};  
  7. listOfNumbers.ToList().ForEach(p => Console.WriteLine(p));  

filter (js) vs Where (LINQ)

 
This method is used to make a filter by a function depending on a condition.

Both methods are pure functions and return new collections including the record that matches the condition.
  1. //JS demo  
  2. const words = ['spray''limit''elite''exuberant''destruction''present'];  
  3. const result = words.filter(word => word.length > 6);  
  4.   
  5. //C# demo  
  6. var listOfNumbers = new int[] {0,1,2,3,4,5};  
  7. var evenNumbers = listOfNumbers.ToList().Where(p => p%2 == 0);  

reduce (js) vs Aggregate (LINQ)

 
This method executes a function for each element to return only one value.

Both methods are pure functions and return a new single value with no affectations in the original collection.
  1. //JS demo  
  2. const array1 = [1, 2, 3, 4, 5];  
  3. const reducer = array1.reduce((accumulator, currentValue) => accumulator + currentValue, 0);  
  4.   
  5. //C# demo  
  6. var listOfNumbers = new int[] {1,2,3,4,5};  
  7. var sumTotal = listOfNumbers.Aggregate(0,(total, currentItem)=> total+ currentItem);  

sort (js) vs OrderBy (LINQ)

 
This method sorts the elements in the collection depending on a function or a parameter.

In JS this method is not a pure function and it updates the original collection. However, in C# this method is a pure function returning a new collection and can sort easily depending on the property selected. Also, you can use the method ‘Then’ to sort the new collection by other properties.
  1. //JS demo  
  2. const months = ['March''Jan''Feb''Dec'];  
  3. months.sort();  
  4.   
  5. //C# demo  
  6. var listOfNumbers = new int[] {1,2,3,4,5};  
  7. var numberSorted = listOfNumbers.OrderBy(p=> p);  
  8. //var listOfUsers = users.OrderBy(p=> p.Name).Then(p=> p.LastName);  

concact (js) vs Concact (LINQ)

 
This method is used to merge into collections into one array.

Both methods are pure functions and return a new collection including the records from the original collection.
  1. //JS demo  
  2. const array1 = [1,2,3,4,5];  
  3. const array2 = [6,7,8,9,10];  
  4. const array3 = array1.concat(array2);  
  5.   
  6. //C# demo  
  7. var listOfNumbers = new int[] {1,2,3,4,5};  
  8. var listOfNumbers2 = new int[] {6,7,8,9,10};  
  9. var allNumbers = listOfNumbers.Concat(listOfNumbers2);  

push (js) vs Append (LINQ)

 
This method is used to add a new element to add the end of the list. Both methods update the original collection.
  1. //JS demo  
  2. var array1 = [1,2,3,4,5];  
  3. array1.push(6);  
  4.   
  5. //C# demo  
  6. var listOfNumbers = new int[] {1,2,3,4,5};  
  7. listOfNumbers.Append(6);  

find (js) vs FirstOrDefault (LINQ)

 
This returns the first item which satisfies the criteria specified. These methods don’t affect or update the original collection (pure functions).
  1. //JS demo  
  2. var array1 = [1,2,3,4,5];  
  3. var item1 = array1.find(p=> p===1);  
  4.   
  5. //C# demo  
  6. var listOfNumbers = new int[] {1,2,3,4,5};  
  7. var item1 = listOfNumbers.FirstOrDefault(p=> p==1);  

includes (js) vs Any (LINQ)


This method returns a boolean whether an array contains an element in JS. In LINQ, the method ‘Any’ returns a boolean whether at least an element satisfies the condition specified. These methods don’t affect or update the original.
  1. //JS demo  
  2. var array1 = [1,2,3,4,5];  
  3. var result = array1.includes(1);  
  4.   
  5. //C# demo  
  6. var listOfNumbers = new int[] {1,2,3,4,5};  
  7. var result = listOfNumbers.Any(p=> p==1);  

reverse (js) vs Reverse (LINQ)

 
This method in JS reverses an array by modifying the original array and also returning a new one reversed. In LINQ this method is a pure function and returns a new array reversed with no affections on the original collection.
  1. //JS demo  
  2. var array1 = [1,2,3,4,5];  
  3. var newArray = array1.reverse(); //array1 is reversed  
  4.   
  5. //C# demo  
  6. var listOfNumbers = new int[] {1,2,3,4,5};  
  7. var newListOfNumbers = listOfNumbers.Reverse();