Toán tử += (Add and Assignment)
Toán tử += giúp viết code gọn hơn: tot += a[i] tương đương tot = tot + a[i]. Rất hữu ích khi xử lý code dài.
Ví dụ
<html> <body> <script> var tot = 0; var a = [1,45,78,9,78,40]; for(var i = 0; i < a.length; i++){ tot += a[i] } document.write(tot); </script> </body> </html>
Kết quả
251