Agyan
adda
Run
Reset
Copy
JavaScript Tutorial › JS Functions › Function with parameters and return
Back to tutorial
Code
Edit, then press Run or Ctrl+Enter
<pre id="out"></pre> <script> function calcTotal(price, qty = 1, gst = 18) { const subtotal = price * qty; return subtotal + subtotal * gst / 100; } document.getElementById('out').textContent = 'Pen x 1: ₹' + calcTotal(10) + '\n' + 'Book x 3: ₹' + calcTotal(200, 3) + '\n' + 'Bag x 2 (12% GST): ₹' + calcTotal(500, 2, 12); </script>
Result