Hi dev,
In this article we will learn example about Show or print Sum of Arguments using javascript. Let's start …
Syntax & Example:
sum-of-arguments.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>sum-of-arguments</title>
<link rel="stylesheet" type="text/css" href="../style.css" />
</head>
<body>
<h1>14-sum-of-arguments!</h1>
<h3>Write a function which show or print Sum of Arguments passed</h3>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
Syntax & Example:
script.js
console.log('14-sum-of-arguments');
function sumOfArguments(...items){
// rest operator converts anything passed as an array
console.log('current items/values to add:', items);
return items.reduce((n1, n2) => n1 +n2);
}
console.log('Sum:', sumOfArguments(10, 2, 8, 4, 6));
// console.log('Sum:', sumOfArguments([10, 2, 8, 4, 6]));
Hope it can help you…
Categories : JavaScript
Tags : HTML