### Eric Sjöström Jennerstrand

Litium E-commerce, C#, .NET MAUI, Javascript, React, Azure, Git. .NET, Node and more!

# Adding Numbers in Javascript: Code Examples

Introduction to Adding Numbers in Javascript. JavaScript is a versatile and widely used programming language, especially in the realm of web development. It empowers developers to create dynamic and interactive web pages and applications. One of the fundamental operations in JavaScript is adding numbers, and it plays a crucial role in various applications across different domains. In this article, we will delve into the fascinating world of adding numbers in JavaScript, exploring not only basic addition but also more advanced concepts like adding arrays and dealing with decimals.

When it comes to basic addition in JavaScript, the language provides simple and intuitive syntax. You can add numbers using the addition operator (+), which works seamlessly with numeric data types. For instance, you can add two numbers together by simply writing their corresponding variables or values separated by the plus sign. JavaScript’s dynamic nature allows for flexibility in adding both integer and floating-point numbers, accommodating a wide range of mathematical operations.

=== Examples of Adding Numbers in Javascript ===

Adding two numbers in Javascript is a straightforward operation. Here is an example:

``````let num1 = 5;
let num2 = 7;
let sum = num1 + num2;
console.log(sum); // Output: 12``````

In this example, we declare two variables `num1` and `num2`, which represent the numbers we want to add. We then declare a variable `sum` and assign it the value of `num1 + num2`. Finally, we log the value of `sum` to the console.

We can also add arrays of numbers in Javascript. Here is an example:

``````let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let sum = arr1.reduce((acc, curr) => acc + curr) + arr2.reduce((acc, curr) => acc + curr);
console.log(sum); // Output: 21``````

In this example, we declare two arrays `arr1` and `arr2`, which represent the arrays of numbers we want to add. We then use the `reduce` function to add the numbers in each array, and then add the results together to get the final sum. Finally, we log the value of `sum` to the console.

``````let num1 = 1.5;
In this example, we declare two variables `num1` and `num2`, which represent the decimal numbers we want to add. We then declare a variable `sum` and assign it the value of `num1 + num2`. Finally, we log the value of `sum` to the console.