Skip to content
the poor coder

summation js

Poor Man
Published
Reading time
1 min read

Summation in JavaScript is a process of adding up multiple numbers or other values. The most common way to do this is with the JavaScript "+" operator. For example, if we want to add up the numbers 3, 5, and 7, we could do:

Text
var sum = 3 + 5 + 7;
console.log(sum); // 15

This would output the value 15 to the console. However, the "+" operator isn't the only way to add up a series of values. You could also use the Array.reduce() method to do summation. This method takes a function as an argument, and the function is applied to each element of the array and the result is accumulated. For example:

Text
var numbers = [3, 5, 7];
var sum = numbers.reduce(function(a, b) {
  return a + b;
});
console.log(sum); // 15

In this example, the function provided to reduce() simply adds the two numbers passed to it and returns the result. This way of doing summation allows for more flexibility, as the function can be changed to do more complicated operations. Finally, there is also the Array.prototype.sum() method which is a shorthand for doing summation. This method is available in modern browsers, but not in Node.js. To use it, we can do:

Text
var numbers = [3, 5, 7];
var sum = numbers.sum();
console.log(sum); // 15

This is the simplest way to do summation, as it requires no additional function. As the name implies, this method is specific to summing up an array of numbers, and will not work for any other type of input.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC